First time here? Check out the FAQ!

Revision history  [back]

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'

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

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

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) # 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? 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) 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'

'done'

​