Python中的线程/队列 [英] Threading / Queue in Python

查看:85
本文介绍了Python中的线程/队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算在python 2.5.2中使用线程/队列 但是似乎python在queue.join()命令处冻结了. followong代码的输出仅为:BEFORE

I intend using threads/queues with python 2.5.2 But it seems that python becomes freezed at the queue.join()-command. The output of the followong code is only: BEFORE

import Queue
import threading

queue = Queue.Queue()

class ThreadUrl(threading.Thread):

    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue

    def run(self):
        while True:

            i = self.queue.get()
            print i
            self.queue.task_done()


def main():

    for i in range(5):
        t = ThreadUrl(queue)
        t.setDaemon(True)
        t.start()

    for i in range(5):
        queue.put(i)

    print "BEFORE"
    queue.join()
    print "AFTER"


main()

有人对出什么问题有想法吗?

Has someone an idea about what is going wrong?

推荐答案

我认为这是t.setDaemon(True)部分.

因此在Python> 2.6中,使用:

So in Python > 2.6, use:

t.setDaemon(True)

在Python中< 2.6,使用:

And in Python < 2.6, use:

t.daemon = True

这篇关于Python中的线程/队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆