[Tutor] Multiprocessing

Cameron Simpson cs at cskk.id.au
Tue Sep 22 06:18:04 EDT 2020


On 22Sep2020 05:56, Stephen M Smith <stephen.m.smith at comcast.net> wrote:
>I added your suggested line of code (in first test after the print statement
>and then both before and after)  and get the same result. I also shut my
>system down and restarted it and pycharm to make sure everything was as
>clean as possible.Thanks.

I've done somw more reading. Are you on Windows (guessing so from the 
"Outlook" mailer header and the double spacing of the pasted text)?

On Windows the default subprocess mechanism is "spawn". This spawns a 
new Python interpreter and imports your module again. The implication of 
this is that the module source code is rerun in the child process.

So, what's going on there?

All the unconditional code in your module is run in each child process.  
So your leading print() call is run again every time.

However, the child processes are not running your module as the "main 
programme" (detected by __name__ == '__main__'). So in the child 
process, the code _inside_ the "if __name__ == '__main__'" is _not_ run.

So the design rule here is: do nothing with side effects before the 
"if-main" part. Define the do_something function (because you need that; 
it is what you're asking to be run), but don't do any print()s - the 
produce output (a side effect).

Does this clarify things?

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list