Why doesn't Queue work?


131
views
0
4 weeks ago by
zz  
from multiprocessing import Process, Queue
import os, time, random

def write(q):
print ('Process to write: %s' % os.getpid())
a=33
q.put(a)

def read(q):             # It seems that the function doesn't work
print ('Process to read: %s' % os.getpid())
while True:
value = q.get(True)
print ('Get %s from queue.' % value)

if __name__=='__main__':
q = Queue()
pw = Process(target=write, args=(q,))
pr = Process(target=read, args=(q,))
pw.start()
pr.start()
pw.join()
pr.terminate()
print ('done')

the output:
Process to write: 1880
done
Community: Wing Python IDE

3 Answers


2
29 days ago by
Interesting, I've replicated this now also.  It works intermittently and does not work intermittently.  This is true also when run from the DOS Console.  The cause is that there is no pr.join() so the parent process can exit before the child running read() has time to do anything.  Here is corrected code, which also removes the unneeded while loop in read():

from multiprocessing import Process, Queue
import os, time, random

def write(q):
    print ('Process to write: %s' % os.getpid())
    a=33
    q.put(a)

def read(q):
    print ('Process to read: %s' % os.getpid())
    value = q.get(True)
    print ('Get %s from queue.' % value)

if __name__=='__main__':
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    pw.start()
    pr.start()
    pw.join()
    pr.join()
    pr.terminate()
    pw.terminate()
    print ('done')
​

Actually, terminate() is redundant with join() because join() waits for the process to terminate unless you send in a timeout arg, which you are not doing.  However, I left it in and added one for pw as well, just for completeness. ;-)
0
4 weeks ago by
It works for me.  I get:

Process to write: 38193
Process to read: 38194
Get 33 from queue.
done
I'm assuming I corrected indentation to what you had originally.  You can use the {;} icon to insert code without it getting munged:
from multiprocessing import Process, Queue
import os, time, random

def write(q):
  print ('Process to write: %s' % os.getpid())
  a=33
  q.put(a)

def read(q):             # It seems that the function doesn't work
  print ('Process to read: %s' % os.getpid())
  while True:
    value = q.get(True)
    print ('Get %s from queue.' % value)

if __name__=='__main__':
  q = Queue()
  pw = Process(target=write, args=(q,))
  pr = Process(target=read, args=(q,))
  pw.start()
  pr.start()
  pw.join()
  pr.terminate()
  print ('done')​

If it still does not work for you some more information that "does not work" would be helpful in diagnosing this...
0
4 weeks ago by
zz  
Later I run the code in Spyder,  in Pycharm ( latest edition ),
The results are always the same.   the interpreter is Python 2.7.   Win7 x64

Then I run it in Wing IDE in my VMware Workstation , the result is still the same! the interpreter is Python 3.6.1.   Win7 x64

Another strange thing I found  after I modified the code slightly as follows is that the run results in Pycharm is right----read function dose work if let it sleep for a while after put. But it's still incorrect in Wing and Spyder.

from multiprocessing import Process, Queue
import os, time, random

def write(q):
print ('Process to write: %s' % os.getpid())
for value in ['A', 'B', 'C']:
print('Put %s to queue...' % value)
q.put(value)
# time.sleep(random.random())  #if it's commented out,the result of Pycharm is wrong,otherwise the result is right. So the reason seems to be the conflict of print between processes, or the read process is terminated when the write ends. However,no matter what I do,it still doesn't work in Wing.Why?

def read(q): # It seems that the function doesn't work
print ('Process to read: %s' % os.getpid())
while True:
value = q.get(True)
print 'Get %s from queue.' % value
time.sleep(1)

if __name__=='__main__':
q = Queue()
pw = Process(target=write, args=(q,))
pr = Process(target=read, args=(q,))
pw.start()
pr.start()
pw.join()
time.sleep(1)
pr.terminate()
print 'done'

​
Please login to add an answer/comment or follow this question.

Similar posts:
Search »