[Tutor] Multiprocessing
Stephen M Smith
stephen.m.smith at comcast.net
Tue Sep 22 08:03:05 EDT 2020
OK but I why does it run 17 times.? There were 10 copies fired up in the
version you saw (I think) so I sort of get why it would run once originally
and 1 more time for each of the multiprocessing jobs, but that only gets me
to 11. 17 is not a number I can resolve.
-----Original Message-----
From: Cameron Simpson <cs at cskk.id.au>
Sent: Tuesday, September 22, 2020 7:29 AM
To: Stephen M Smith <stephen.m.smith at comcast.net>
Cc: tutor at python.org
Subject: Re: [Tutor] Multiprocessing
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