why does my print function (in multiprocessing) print nothing?

谁都会走 提交于 2019-12-12 13:34:27

问题


why does my print function (in python multiprocess) print nothing?

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())


def read(q):
    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()
    print('start')

    pr.start()

    pw.join()

    pr.terminate()
    print('end')

I run it on spyder (windows 10 system).

My result on IPython console of Spyder:

runfile('C:/Users/Dust/Desktop/programs/crawl/test.py', wdir='C:/Users/Dust/Desktop/programs/crawl')
start
end

Result on Python console of Spyder:

>>> runfile('C:/Users/Dust/Desktop/programs/crawl/test.py', wdir='C:/Users/Dust/Desktop/programs/crawl')
start
Process to write: 12824
Put A to queue...
Put B to queue...
Put C to queue...
end

It is really weird. The results are different, but both are not what I want.

Could anyone help me find where is the problem in my program. Thanks a lot

来源:https://stackoverflow.com/questions/45587101/why-does-my-print-function-in-multiprocessing-print-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!