Python Full stack learning--day39 (multiprocess module)

Source: Internet
Author: User
Tags terminates

Multiprocess Module

Multiprocess is not a module but a package of operations, management processes in Python. The reason is called Multi is taken from the multiple multi-functional meaning, in this package contains almost all the sub-modules related to the process.

Due to the large number of sub-modules provided, in order to facilitate the classification of memory, I will be roughly divided into four parts: the creation process part, the process synchronization part, the process pool part, the process data sharing.

Multiprocess.process Module Process Module Introduction

The process module is a module that creates a process that enables the creation of processes.

Process ([group [, Target [, name [, args [, Kwargs]]]), an object instantiated by the class that represents a task in a child process (not yet started) emphasizes: 1. You need to use a keyword to specify parameter 2. args Specifies the positional parameter passed to the target function, which is a tuple form and must have a comma argument: the group parameter is not used, the value is always nonetarget to represent the calling object, that is, the task that the child process is executing, the args represents the location parameter tuple of the calling object, args= ( Kwargs, ' Egon ',) represents the dictionary of the calling object, kwargs={' name ': ' Egon ', ' age ': 18}name is the name of the child process
1 P.start (): Starts the process and invokes the P.run () 2 P.run () in the subprocess: The method that runs at the start of the process, is exactly what it goes to call the function specified by the target, we must implement the method in the class of the custom Class 3 p.terminate (): Force terminate Process P, No cleanup is done, and if p creates a child process, the subprocess becomes a zombie process, and using this method requires special care. If P also holds a lock then it will not be released, resulting in a deadlock. 4 p.is_alive (): If P is still running, return True5 p.join ([timeout]): The main thread waits for p to terminate (emphasis: is the main thread is in the state, and P is in the running state). Timeout is an optional time-out, and it should be emphasized that the p.join can only join the START process and not join the run-open process
1 P.daemon: The default value is False, if set to true, for p to run in the background daemon, when P's parent process terminates, when P's parent process terminates, p also terminates, and set to True, p cannot create its own new process, must be set before P.start () 2 P.name: Process name 3 p.pid: Process Pid4 P.exitcode: process is none at run time, if is-N, Signal N is ended (understand) 5 P.authkey: Process authentication key, default is by Os.urandom () A randomly generated 32-character string. The purpose of this key is to provide security for the underlying interprocess communication that involves a network connection, which can only succeed if you have the same authentication key (understand it)
In the Windows operating system, because there is no fork (the mechanism of creating a process in the Linux operating system), when the child process is created, it will automatically import to launch its file, and in the import when the entire file is executed. Therefore, if the process () is written directly in the file, it will be infinitely recursive to create the child process error. Therefore, you must use the IF __name__ = = ' __main__ ' to determine the part of the child process to protect it, and when  you import it, it will not run recursively.
To create a process using the process module

Turns on child processes, start methods, and concurrency effects in a python process.

Import Time fromMultiprocessingImportProcessdeff (name):Print('Hello', name)Print('I am the child process')if __name__=='__main__': P= Process (Target=f, args= ('Bob',)) P.start () Time.sleep (1)    Print('executes the contents of the main process')

  

  

  

Python Full stack learning--day39 (multiprocess module)

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.