D..
D..

Reputation: 39

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

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

Upvotes: 3

Views: 2087

Answers (1)

Dirk Helgemo
Dirk Helgemo

Reputation: 59

The multiprocessing module uses fork to spawn child processes for parallelism. Windows does not implement fork, and Python emulation of it on Windows is incomplete. One of the missing effects is that stdout (used by print()) is not inherited by the child processes. Rather, child processes use the default stdout.

This works fine in a command window because it uses default stdout. This shows nothing in Spyder because a different pipe is used for output to the Spyder IPython console window.

See this question ("Simple Multiprocessing function in Spyder doesn't output results") and this answer for additional details and potential mitigations.

Upvotes: 1

Related Questions