How to run a process in the background
Running a process in background is a typical mode of execution of laborious processes that do not require your presence or intervention, and this course may be concurrent to the execution of other programs. The Python multiprocessing module allows us, through the daemonic option, to run background processes.
How to do it...
To run a background process, simply follow the given code:
import multiprocessing
import time
def foo():
name = multiprocessing.current_process().name
print ("Starting %s \n" %name)
time.sleep(3)
print ("Exiting %s \n" %name)
if __name__ == '__main__':
background_process = multiprocessing.Process\
(name='background_process',\
target=foo)
background_process.daemon = True
NO_background_process = multiprocessing.Process\
(name='NO_background_process',\
...
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime