Python Enthought Canopy: multiprocessing not worki

2019-02-21 05:55发布

问题:

I've been trying multiprocessing with enthought canopy (Windows 8). I tried the following example:

import multiprocessing

nProcesses=3

def worker():
   """worker function"""
    print "working"
    return

if __name__ == '__main__':
 jobs = []
 for i in range(nProcesses):
     p = multiprocessing.Process(target=worker)
     jobs.append(p)
     p.start()

close to a copypaste of examples you find online...

The processes are created but seem to do nothing. No printing of "working".

I run my file (main.py) from the environment provided by Canopy (IDLE I think) but I do not copy those lines in the interpreter, I run the whole script (like %run "D:/path/main.py")

What am I doing wrong?

回答1:

Canopy's python shell is IPython's QtConsole (not IDLE).

QtConsole separates the calculations from the console output (front end). To ensure that text is printed when you want, insert this after your print statement:

sys.stdout.flush()