Python full stack day 34th------Two ways to open a process, join method, process object Other related properties and methods, zombie process, orphan process, daemon, mutex

Source: Internet
Author: User
Tags unique id

First, two ways to open the process

Way One:

From multiprocessing import Process
Import time

def task (name):
Print ('%s is running '%name)
Time.sleep (3)
Print ('%s is do '%name)

# on Windows systems, the operation to turn on a subprocess must be placed in the sub-code of the If __name__ = = ' __main__ '
if __name__ = = ' __main__ ':
P=process (target=task,args= (' Egon ',)) #Process (target=task,kwargs={' name ': ' Egon '})
P.start () # just sends a signal to the operating system to turn on the sub-process
Print (' master ')
Way two:
From multiprocessing import Process
Import time

Class Myprocess (Process):
def __init__ (self,name):
Super (). __INIT__ ()
Self.name=name

def run (self):
Print ('%s is running '%self.name)
Time.sleep (3)
Print ('%s is do '%self.name)

# on Windows systems, the operation to turn on a subprocess must be placed in the sub-code of the If __name__ = = ' __main__ '
if __name__ = = ' __main__ ':
P=myprocess (' Egon ')
P.start () # just sends a signal to the operating system to turn on the sub-process
Print (' master ')
Second, join method
Let the main process wait in place, waiting for the child process to run, without affecting the execution of the child process
Third, Process object related properties
Process PID: Each process has a unique ID number within the operating system, called a PID
Iv. Zombie process and orphan process
Zombie Process: A process uses fork to create a child process, and if the child process exits, and the parent process does not call wait or waitpid to get state information for the child process, the process descriptor of the child process is still stored in the system. This process is called a zombie process.
Orphan process: One parent process exits, and one or more of its child processes are still running, then those child processes will become orphans. The orphan process will be adopted by the INIT process (process number 1) and the Init process completes the state collection for them.
Guardian Process: The essence is a ' child process ', the life cycle of the ' subprocess ' is <= by the life cycle of the daemon process
Six, mutual exclusion lock
Using the Mutex.acquire () method to realize the serial effect under concurrency

Python full stack day 34th------Two ways to open a process, join method, process object Other related properties and methods, zombie process, orphan process, daemon, mutex

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.