Processes and threads supported by the Python-cpython interpreter

Source: Internet
Author: User
Tags terminates

One. Python concurrent programming Multi-process 1. Multiprocessing Module Introduction
Multithreading in Python does not take advantage of multicore advantages, 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 to be used. Python provides a very useful multi-process package 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, providing process, Queue, Pipe, lock and other components.

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.
2. Introduction to the Process class

To create a process class:

Process ([group [, Target [, name [, args [, Kwargs]]]), an object instantiated by the class representing a task in a child process (not yet started)

Emphasize:

1. Need to use keywords to specify parameters

2.ARGS Specifies the positional parameter passed to the target function, which is a tuple form and must have a comma

Parameter description:

The group parameter is not used, and the value is never nonetarget to represent the calling object, that is, the task that the child process is to perform. Args represents the positional parameter tuple of the calling object, args= ("Egon"  Kwargs represents the dictionary of the calling object, Kwargs={'name':'Egon', '  Age ':}name is the name of the child process

Method Description:

P.start (): Starts the process and calls the P.run () P.run () in the subprocess: The method that runs at the start of the process is exactly what he calls the function specified by the target, and we must implement the method in the class of our custom Class P.terminate (): Force terminate Process P, No cleanup is done, and if p creates a child process, the subprocess becomes a zombie process, which is especially careful with this method. If P also holds a lock then it will not be released, resulting in Deadlock p.is_alive (): If P is still running, return truep.join ([timeout]): The main process waits for P to terminate (emphasis: is the state in which the main process is in, 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

Property Description:

P.daemon: The default value is False, if set to True, represents the daemon that P is running in the background, when P's parent process terminates, p also terminates with it, and when set to True, p cannot create its own new process, it must be in the P.start () Before setting P.name: Process name p.pid: Process Pidp.exitcode: Process is none at run time, if -
3. Use of the Process class

Two ways to create and open a child process

Note: In Windows, process () must be placed in the#if __name__ = = ' __main__ ': UnderSince Windows has no fork, the multiprocessing module starts a new Python process andimports the calling module. If Process () gets called uponImport, then this sets off an infinite succession of new processes (oruntil your machine runs out of resources). this isThe reason forhiding calls to Process () insideif __name__=="__main__"since statements inside thisif-statement would notGet called uponImport.

Because Windows does not have fork, the multi-processing module launches a new Python process and imports the calling module.
If you call process () on import, this will start a new process with infinite inheritance (or until the machine runs out of resources).
This is the original to hide the internal call to process (), using if name = = "main ", the statement in this if statement will not be called at the time of import.

One way to open a process:

Import timeimport randomfrom multiprocessing import processdef Piao (name):    print ('%s piaoing '%name)    Time.sleep (Random.randrange (1,5))    print ('%s Piao end '%name) p1=process (target=piao,args= (' Egon ',)) #必须加, number p2= Process (target=piao,args= (' Alex ',)) p3=process (target=piao,args= (' Wupeqi ',)) p4=process (target=piao,args= (' Yuanhao ',)) P1.start () P2.start () P3.start () P4.start () print (' main thread ')

How to open a process two

Import timeimport randomfrom multiprocessing import processclass Piao (Process):    def __init__ (self,name):        Super (). __init__ ()        self.name=name    def run (self):        print ('%s piaoing '%self.name)        time.sleep ( Random.randrange (1,5))        print ('%s Piao end '%self.name) P1=piao (' Egon ') P2=piao (' Alex ') P3=piao (' Wupeiqi ') P4=piao (' Yuanhao ') P1.start () #start会自动调用runp2. Start () P3.start () P4.start () print (' main thread ')

  

Processes and threads supported by the Python-cpython interpreter

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.