Open szcf-weiya opened 4 years ago
refer to https://pymotw.com/2/multiprocessing/basics.html#importable-target-functions
Due to the way the new processes are started, the child process needs to be able to import the script containing the target function. Wrapping the main part of the application in a check for __main__
ensures that it is not run recursively in each child as the module is imported. Another approach is to import the target function from a separate script.
existing output
$ python daemon.py
Starting: daemon 19533
Starting: non-daemon 19534
Existing: non-daemon 19534
time.sleep(3)
, the existing message will appear
$ python daemon.py
Starting: daemon 19907
Starting: non-daemon 19908
Existing: non-daemon 19908
Existing: daemon 19907
d.join()
, which would wait for the daemon to exit
$ python daemon_join.py
Starting: daemon 30015
Starting: non-daemon 30016
Existing: non-daemon 30016
Existing: daemon 30015
d.is_alive() = False
d.join(1)
with fewer seconds than the required to finish the job, then the daemon existing message would disappear and d
is still alive
$ python daemon_join.py
Starting: daemon 30352
Starting: non-daemon 30357
Existing: non-daemon 30357
d.is_alive() = True
starmap
ntasks = ncpus
several tries
![image](https://user-images.githubusercontent.com/13688320/110572794-19eced80-8195-11eb-8f73-80d58f2be0ea.png)
ntasks = 2*ncpus