First time here? Check out the FAQ!
1

Why doesn't Queue work?

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,)) p
    r = Process(target=read, args=(q,)) 
    pw.start() 
    pr.start() 
    pw.join() 
    pr.terminate() 
    print ('done')

the output:

Process to write: 1880
done
zz's avatar
11
zz
asked 2018-01-25 07:41:00 -0600
Wingware Support's avatar
4.2k
Wingware Support
updated 2019-03-13 08:49:02 -0600
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

3 Answers

1

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. ;-)

Wingware Support's avatar
4.2k
Wingware Support
answered 2018-01-26 09:23:00 -0600
Wingware Admin's avatar
231
Wingware Admin
updated 2019-03-07 08:54:21 -0600
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments
0

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...

Wingware Support's avatar
4.2k
Wingware Support
answered 2018-01-25 08:40:00 -0600
Wingware Admin's avatar
231
Wingware Admin
updated 2019-03-07 08:52:59 -0600
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments
0

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 x64Another 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'
zz's avatar
11
zz
answered 2018-01-26 03:50:00 -0600
Wingware Admin's avatar
231
Wingware Admin
updated 2019-03-07 08:53:54 -0600
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss.

Add Answer