[Tutor] Multiprocessing
Cameron Simpson
cs at cskk.id.au
Tue Sep 22 07:28:56 EDT 2020
On 22Sep2020 06:57, Stephen M Smith <stephen.m.smith at comcast.net> wrote:
>Thanks - I don't get all of the subtleties of python yet. I am picking it up
>after a long career in much older technology. I don't think in python yet -
>may never. In any event I understand what has happened and the change to
>make, don't really understand the why.
It is only partly Python; the other part is how the multiprocessing
module is implemented.
When you "import" a module - any of those "import" statements at the top
of the script, the code which implements the module is actually run at
that time. It only happens once - importing the same module again
elsewhere just uses the already imported module.
The main programme you've written is itself a small module, whose name
is "__main__", which is what the if-statement down the bottom checks.
That magic peice of boilerplate code lets you write modules which can
run as a standalone programme. If you import it, it just defines the
various functions and whatnot. But if you run it as a main programme, it
is imported with the special name "__main__", and the if-statement at
the bottom actually runs the main programme, _after_ defining functions
and whatnot.
So, what has this to do with multiprocessing?
When you kick off a subprocess with the multiprocessing module, it
starts a shiny new Python programme for the subprocess, and has that
import your code. As a module. But _not_ named "__main__"!
So when you start your programme, it runs as "__main__" and the stuff
inside the if-statement at the bottom runs in addition to all the stuff
before.
Each subprocess _also_ runs your script, but not as "__main__". So it
runs the stuff before the if-statement (to define the functions and
whatnot), but skips the stuff inside the if-statement.
In this way the subprocess loads your functions, so that it can be told
to run one of them.
Because your first print() call is outside the if-statement, it is part
of the stuff which is always executed. So it runs once for you in the
main programme, and also by every subprocess.
Cheers,
Cameron Simpson <cs at cskk.id.au>
More information about the Tutor
mailing list