Main content:
# Process
# 1. Data isolation issues between processes
# 2. Daemon process
# several process models----process-synchronized tools
# sequential is the synchronization
# Between processes is asynchronous
# want asynchronous multi-process operation, maintain one order--synchronization tool
# 3. Locking Lock * * * * *
# 4. Semaphore Semaphore * * *
# 5. Events Event * *
1. Data isolation issues between processes
fromMultiprocessingImportPROCESSN= 100defFun ():GlobalN N-= 1returnNif __name__=='__main__': P= Process (target=Fun ) P.start ()Print(n)"""# The data between the process and the process is isolated # memory space is not shared # so if you want to communicate, you have to use other means # and both processes are voluntary # The result of the execution of the child process the parent process gets the result of the execution of the child process dependent on the parent process. # How the parent process gets the result of the child process??? # Communication between parent and child processes via socket"""
2. Daemon Process
# What happens after you set it up as a daemon?
# The daemon will end directly after the execution of the main process's code, regardless of whether the daemon has finished executing
fromMultiprocessingImportProcessImport TimedefFun ():Print("Start") Time.sleep (3) Print("It's a fine day.")if __name__=='__main__': P= Process (target=Fun ) P.daemon=True#The Daemon's properties, the default is False, if set to true, indicates that the child process is set as a daemon #the setting daemon should be preceded by a child processP.start () time.sleep (1) Print("The main process is over!") #What happens after you set it up as a daemon? #The daemon will end directly after the code of the main process finishes, regardless of whether the daemon finishes executing
An example of a need to note about the daemon is as follows: (note that the in fun2 of the following fun2 only runs two times, and is followed by the main function)
Import Time fromMultiprocessingImportProcessdefFun ():Print("Start") Time.sleep (3) Print("It's a nice day.")deffun2 (): whileTrue:Print("In fun2") Time.sleep (0.5)if __name__=='__main__': P= Process (target=Fun ) P.start () p= Process (target=fun2) P.daemon=True P.start () time.sleep (1) Print("The main program is over.")"""the results of the operation are as follows: In Fun2startin fun2 the main program is over. It's a nice day."""
The following functions are mainly used to observe and expand applications:
# reporting the Living master process is alive # 100 Machines 100 Processes 10000 processes # whether the app is working properly-task Manager to view # How the daemon reports live to the monitoring mechanism??? send/Write a database # Why do you use a daemon to report live? Why not work with the main process??? # The daemon does not consume CPU or require the operating system to dispatch # The main process can send a message every 60s strictly
Process data isolation, daemons, locks, semaphores, and events