Chinaunix首页 | 论坛 | 博客
  • 博客访问: 236664
  • 博文数量: 52
  • 博客积分: 1492
  • 博客等级: 上尉
  • 技术积分: 554
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-09 15:54
文章分类
文章存档

2010年(52)

我的朋友

分类: Python/Ruby

2010-08-11 22:14:26

例:
 
 

for i in range(num_threads):
    worker = Thread(target=pinger, args=(i, queue))
    worker.setDaemon(True)
    worker.start()

 

官档上是这样解释的:

 

setDaemon()

 

    Old API for daemon.

 

    A boolean value indicating whether this thread is a daemon thread (True) or not (False). This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.

 

    The entire Python program exits when no alive non-daemon threads are left.

个人理解:

setDaemon(True)将线程声明为守护线程,必须在start() 方法调用之前设置,如果不设置为守护线程程序会被无限挂起。

The entire Python program exits when no alive non-daemon threads are left

上面这句话的意思是:当没有存活的非守护进程时,整个python程序才会退出。

也就是说:如果主线程执行完以后,还有其他非守护线程,主线程是不会退出的,会被无限挂起;必须将线程声明为守护线程之后,如果队列中的数据运行完了,那么整个程序想什么时候退出就退出,不用等待

参考:
 

 

 

阅读(9025) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~