Reputation: 1
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?
Upvotes: -1
Views: 271
Reputation: 19184
The current explanation in the IDLE doc section "Running User Code":
By default, IDLE runs user code in a separate OS process rather than in the user interface process that runs the shell and editor. In the execution process, it replaces sys.stdin, sys.stdout, and sys.stderr with objects that get input from and send output to the Shell window. The original values stored in sys.stdin, sys.stdout, and sys.stderr are not touched, but may be None.
...
IDLE’s standard stream replacements are not inherited by subprocesses created in the execution process, whether directly by user code or by modules such as multiprocessing. If such subprocess use input from sys.stdin or print or write to sys.stdout or sys.stderr, IDLE should be started in a command line window. (On Windows, use python or py rather than pythonw or pyw.) The secondary subprocess will then be attached to that window for input and output.
I started IDLE in Windows Command Prompt with py -m idlelib
. Then I pasted Zokalwe's code (after editing) into Editor and ran it. The expected output 'working', appeared thrice in Command Prompt, after the command line.
Other GUI IDEs tend to have the same issue.
Upvotes: 0
Reputation: 5810
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()
Upvotes: 1