python - 在 python 中结束线程的生命?

标签 python multithreading

我有以下代码,但它在队列为空后仍然存在,任何见解:

def processor():
   while(1>0):
    if queue.empty() == True:  
    print "the Queue is empty!"  
    break
   source=queue.get()
   page = urllib2.urlopen(source)
   print page   

def main:
   for i in range(threads):  
    th = Thread(target=processor)  
    th.setDaemon(True)  
    th.start()  
   queue.join()  

它打印 queue empty 的次数与我有线程的次数一样多,只是站在那里什么都不做。

最佳答案

打印页面后需要调用queue.task_done(),否则join()会阻塞。每个线程在使用 get() 之后必须调用 task_done()。

参见 documentation for queue

关于python - 在 python 中结束线程的生命?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6227058/

相关文章:

python - 图像在 Pygame 中旋转,同时保持平滑的边缘

python - "ImportError: module ' xxxxxxxx ' has no attribute ' 主要 '"将项目移植到 Heroku 时

python - 思考特征重要性的不同方式

python - 在同一个 python 应用程序上托管多个网站

多线程:线程多于内核有什么意义?

c# - 为什么 System.Threading.Timer 回调成功更新 UI?

c# - SpinLock.Enter 获取锁失败怎么办?

python - Python 中的 GString

java - 多线程等待批量操作

multithreading - 如何将 Condvar 与 RwLock 一起使用?