The following code is about the communication between two child processes by queue. I can not figure it out that why the self.q.qsize() is zero and self.q.get() is blocked in the Function "ACT"???.... Since the counter shows the queue is full...
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from multiprocessing import Process
import multiprocessing
import time
from Queue import Queue
class Test():
def __init__(self):
self.q = Queue(1)
def ACT(self):
while True:
print "B momoent queue size: %s" %self.q.qsize()
print self.q.get()
time.sleep(1)
def counter(self):
for i in range(5,10):
if not i == 5:
print "Hello1"
print self.q.full()
self.q.join()
print "Hello2"
self.q.put(str(i))
print "A moment queue size: %s" % self.q.qsize()
if __name__ == '__main__':
foo = Test()
qw = Process(target=foo.counter)
qw.start()
qr = Process(target=foo.ACT)
qr.start()
qw.join()
print "End"
You need to use multiprocessing.Queue
.
Queue.Queue
is meant for threading use. Right now you are creating and using separate queue instances in each process - there is no logic to facilitate the inter-process communication that multiprocessing.Queue
provides.
multiprocessing.Queue
mirrors most of Queue.Queue
methods but you will have to remove join
method call and possibly qsize
if on OS X.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.