免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1372 | 回复: 3
打印 上一主题 下一主题

python的多线程问题请教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-06-23 09:37 |只看该作者 |倒序浏览
我用的是threading,
在子线程中获取到子线程名称为 thread-1,
然后在子线程中执行 exit ,
然后再在主线程中开启一个子线程,
这时获取到子线程的名称为 thread-2

有点不明白
前面的thread-1明明已经exit了
为什么再开一个子线程,会变成 thread-2?

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
2 [报告]
发表于 2013-06-23 15:31 |只看该作者
文档没说明thread name的生成规则,只有看代码了,截取一段Python 2.7的实现,可以解释你遇到的现象(
threading实现

thread name的规则
  1. 409        # Helper to generate new thread names
  2. 410        _counter = 0
  3. 411        def _newname(template="Thread-%d"):
  4. 412            global _counter
  5. 413            _counter = _counter + 1
  6. 414            return template % _counter
复制代码
thread构造函数
  1. 422        # Main class for threads
  2. 423       
  3. 424        class Thread(_Verbose):
  4. 425       
  5. 426            __initialized = False
  6. 427            # Need to store a reference to sys.exc_info for printing
  7. 428            # out exceptions when a thread tries to use a global var. during interp.
  8. 429            # shutdown and thus raises an exception about trying to perform some
  9. 430            # operation on/with a NoneType
  10. 431            __exc_info = _sys.exc_info
  11. 432            # Keep sys.exc_clear too to clear the exception just before
  12. 433            # allowing .join() to return.
  13. 434            __exc_clear = _sys.exc_clear
  14. 435       
  15. 436            def __init__(self, group=None, target=None, name=None,
  16. 437                         args=(), kwargs=None, verbose=None):
  17. 438                assert group is None, "group argument must be None for now"
  18. 439                _Verbose.__init__(self, verbose)
  19. 440                if kwargs is None:
  20. 441                    kwargs = {}
  21. 442                self.__target = target
  22. 443                self.__name = str(name or _newname())
复制代码

论坛徽章:
0
3 [报告]
发表于 2013-06-24 16:26 |只看该作者
应该不会这么快就重用原来的id吧。

论坛徽章:
0
4 [报告]
发表于 2013-06-24 16:37 |只看该作者
timespace 发表于 2013-06-23 15:31
文档没说明thread name的生成规则,只有看代码了,截取一段Python 2.7的实现,可以解释你遇到的现象(
thr ...


这以前还真不知道,学习了。居然用global来存储。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP