Process data isolation, daemons, locks, semaphores, and events

Source: Internet
Author: User
Tags semaphore

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.