Nếu tôi sử dụng Queue.Queue
, thì chức năng read()
của tôi không hoạt động, tại sao? Nhưng nếu tôi sử dụng multiprocessing.Queue
, nó hoạt động tốt:multiprocessing.Queue và Queue.Queue là khác nhau?
from multiprocessing import Pool, Process, Queue
import os, time
# from Queue import Queue
def write(q):
for v in ['A', 'B', 'C']:
print 'Put %s to queue ' % v
q.put_nowait(v)
time.sleep(0.2)
def read(q):
while 1:
if not q.empty():
v = q.get(True)
print "Get %s from queue" % v
time.sleep(0.2)
else:
break
if __name__ == '__main__':
q = Queue()
pw = Process(target=write, args=(q,))
pr = Process(target=read, args=(q,))
pw.start()
pw.join()
pr.start()
pr.join()
print "all done..."
Hãy suy nghĩ về nó theo cách này: nếu chúng _wer't_ khác nhau, tại sao 'multiprocessing.Queue' thậm chí tồn tại? Toàn bộ điểm của nó là cung cấp cho bạn một đối tượng giống như 'queue.Queue' cũng hoạt động giữa các tiến trình riêng biệt. – abarnert