python 多线程队列未 运行 或干净退出
python multithreading queues not running or exiting cleanly
我正在学习 python 多线程和队列。下面创建了一堆线程,通过队列将数据传递给另一个线程进行打印:
import time
import threading
import Queue
queue = Queue.Queue()
def add(data):
return ["%sX" % x for x in data]
class PrintThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
data = self.queue.get()
print data
self.queue.task_done()
class MyThread(threading.Thread):
def __init__(self, queue, data):
threading.Thread.__init__(self)
self.queue = queue
self.data = data
def run(self):
self.queue.put(add(self.data))
if __name__ == "__main__":
a = MyThread(queue, ["a","b","c"])
a.start()
b = MyThread(queue, ["d","e","f"])
b.start()
c = MyThread(queue, ["g","h","i"])
c.start()
printme = PrintThread(queue)
printme.start()
queue.join()
但是,我只看到第一个线程打印出的数据:
['aX', 'bX', 'cX']
然后没有别的,但是程序没有退出。我必须终止进程才能退出。
理想情况下,在每个 MyThread
进行数据处理并将结果放入队列后,该线程应该退出?同时 PrintThread
应该获取队列中的任何内容并打印出来。
在所有 MyThread
线程完成并且 PrintThread
线程完成处理队列中的所有内容后,程序应该干净地退出。
我做错了什么?
编辑:
如果每个 MyThread
线程都需要一段时间来处理,有没有办法保证 PrintThread
线程在退出之前等待所有 MyThread
线程完成本身?
这样打印线程肯定会处理队列中所有可能的数据,因为所有其他线程都已经退出。
例如,
class MyThread(threading.Thread):
def __init__(self, queue, data):
threading.Thread.__init__(self)
self.queue = queue
self.data = data
def run(self):
time.sleep(10)
self.queue.put(add(self.data))
以上修改将等待 10 秒,然后再将任何内容放入队列。打印线程将 运行,但我认为它退出太早,因为队列中还没有数据,所以程序什么都不打印。
您的 PrintThread
不会循环,而是只会打印出一个队列项目,然后停止 运行。
因此,队列永远不会为空,queue.join()
语句将阻止主程序终止
将 PrintThread
的 run()
方法更改为以下代码,以便处理所有队列项目:
try:
while True:
data = self.queue.get_nowait()
print data
self.queue.task_done()
except queue.Empty:
# All items have been taken off the queue
pass
我正在学习 python 多线程和队列。下面创建了一堆线程,通过队列将数据传递给另一个线程进行打印:
import time
import threading
import Queue
queue = Queue.Queue()
def add(data):
return ["%sX" % x for x in data]
class PrintThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
data = self.queue.get()
print data
self.queue.task_done()
class MyThread(threading.Thread):
def __init__(self, queue, data):
threading.Thread.__init__(self)
self.queue = queue
self.data = data
def run(self):
self.queue.put(add(self.data))
if __name__ == "__main__":
a = MyThread(queue, ["a","b","c"])
a.start()
b = MyThread(queue, ["d","e","f"])
b.start()
c = MyThread(queue, ["g","h","i"])
c.start()
printme = PrintThread(queue)
printme.start()
queue.join()
但是,我只看到第一个线程打印出的数据:
['aX', 'bX', 'cX']
然后没有别的,但是程序没有退出。我必须终止进程才能退出。
理想情况下,在每个 MyThread
进行数据处理并将结果放入队列后,该线程应该退出?同时 PrintThread
应该获取队列中的任何内容并打印出来。
在所有 MyThread
线程完成并且 PrintThread
线程完成处理队列中的所有内容后,程序应该干净地退出。
我做错了什么?
编辑:
如果每个 MyThread
线程都需要一段时间来处理,有没有办法保证 PrintThread
线程在退出之前等待所有 MyThread
线程完成本身?
这样打印线程肯定会处理队列中所有可能的数据,因为所有其他线程都已经退出。
例如,
class MyThread(threading.Thread):
def __init__(self, queue, data):
threading.Thread.__init__(self)
self.queue = queue
self.data = data
def run(self):
time.sleep(10)
self.queue.put(add(self.data))
以上修改将等待 10 秒,然后再将任何内容放入队列。打印线程将 运行,但我认为它退出太早,因为队列中还没有数据,所以程序什么都不打印。
您的 PrintThread
不会循环,而是只会打印出一个队列项目,然后停止 运行。
因此,队列永远不会为空,queue.join()
语句将阻止主程序终止
将 PrintThread
的 run()
方法更改为以下代码,以便处理所有队列项目:
try:
while True:
data = self.queue.get_nowait()
print data
self.queue.task_done()
except queue.Empty:
# All items have been taken off the queue
pass