Multi-process programming of Python concurrent programming

Source: Internet
Author: User
Tags terminates

Multiprocessing Module Introduction The multithreading in Python does not take advantage of multicore, and if you want to fully use the resources of multicore CPUs (Os.cpu_count () Viewing), most of the situations in Python require multiple processes. Python provides the multiprocessing. The multiprocessing module is used to open sub-processes and perform our custom tasks (such as functions) in the subprocess, which is similar to the programming interface of the Multithreaded module threading. The multiprocessing module has many functions: supporting sub-processes, communicating and sharing data, performing different forms of synchronization, and providing components such as process, Queue, Pipe, lock, etc. One thing that needs to be emphasized again is that, unlike threads, processes do not have any shared state, the process modifies data, and changes are limited to that process. Introduction to the Process Class 1, the class that creates the processes: the procedure ([group [, Target [, name [, args [, Kwargs]]]) is instantiated by the class object, indicating that a task in a child process (not yet started) emphasizes: 1. You need to use a keyword to specify that the parameter 2.args specifies the positional parameter passed to the target function, which is a tuple form and must have a comma argument: the 1.group parameter is not used, the value is always none 2.target represents the calling object, which is the task 3 that the child process will perform. Args represents the calling object's positional parameter tuple, args= ("Egon",) 4.kwargs represents the dictionary of the calling object, kwargs={' name ': ' Egon ', ' Age ': 5.name} The name of the child process is described in the method: 1. P.start (): Starts the process and calls the P.run () 2.p.run () in the subprocess: The method that runs at the start of the process, it is the function that calls the target specified, we must implement the method in the class of the custom Class 3.p.terminate (): Force terminate the 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 Deadlock 4.p.is_alive (): If P is still running, return true 5.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 p.join can only join the START process, but not the process properties that are opened by run: 1.P.DAemon: The default value is False, if set to true, which means that P is running in the background daemon, when P's parent process terminates, p also terminates with it, and set to True, p cannot create its own new process, must be set before P.start () 2 p.name: The name of the process 3. P.pid: Process PID 4.p.exitcode: The process is none at run time, if it is –n, indicates that the signal N ends (understand) 5.p.authkey: The process's authentication key, by default, is a 32 character string randomly generated by os.urandom (). 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)

Multi-process programming of Python concurrent programming

Related Article

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.