Advanced Python Programming by Dr. Gabriele Lanaro
Author:Dr. Gabriele Lanaro
Language: eng
Format: mobi, epub
Tags: COM051220 - COMPUTERS / Programming / Parallel, COM051360 - COMPUTERS / Programming Languages / Python, COM051010 - COMPUTERS / Programming Languages / General
Publisher: Packt
Published: 2019-02-28T07:51:29+00:00
In our main program, we start both processes without calling the join() method on either of them at the end of the program. Since p1 is a long-running process, it will most likely not finish executing before p2 (which is the faster process of the two) finishes. We also know that p1 is a daemon process, so our program should exit before it finishes executing. After running the Python script, your output should be similar to the following code:
> python example4.py
Starting process Worker 1, ID 33784...
Starting process Worker 2, ID 33788...
Exiting process Worker 2, ID 33788...
Again, even though the process IDs might be different when you yourself run the script, the general format of the output should be the same. As we can see, the output is consistent with what we discussed: both p1 and p2 processes were initialized and started by our main program, and the program terminated after the nondaemon process exited without waiting for the daemon process to finish.
The ability to terminate the main program without having to wait for specific tasks that the daemon is processing is indeed extremely useful. However, sometimes we might want to wait for daemon processes for a specified amount of time before exiting; this way, if the specifications of the program allow some waiting time for the process' execution, we could complete some potential daemon processes instead of terminating all of them prematurely.
The combination of daemon processes and the join() method from the multiprocessing module can help us implement this architecture, especially given that, while the join() method blocks the program execution indefinitely (or at least until the task finishes), it is also possible to pass a timeout argument to specify the number of seconds to wait for the process before exiting. Let's consider a modified version of the previous example in Chapter13/example5.py. With the same f1() and f2() functions, in the following script, we are changing the way we handle the daemon process in the main program:
# Chapter13/example5.py
Download
Advanced Python Programming by Dr. Gabriele Lanaro.epub
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26538)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19015)
The Fifty Shades Trilogy & Grey by E L James(18971)
Shot Through the Heart by Mercy Celeste(18890)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(16995)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16894)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16817)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16711)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16339)
The Subtle Art of Not Giving a F*ck by Mark Manson(14278)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14083)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13444)
Scorched Earth by Nick Kyme(12721)
Drei Generationen auf dem Jakobsweg by Stein Pia(10928)
Suna by Ziefle Pia(10852)
Scythe by Neal Shusterman(10282)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9486)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9331)
This is Going to Hurt by Adam Kay(9114)
