Python 3 programming Multi-process multiprocessing module

Source: Internet
Author: User

Tag: Use TCO to end ALS mod sync save build name

First, 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 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.

Ii. Introduction to the Process class

To create a process class:

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 to be 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 always nonetarget for the calling object, that is, the task that the child process is executing. Args represents the positional parameter tuple of the calling object, args= ("Egon"  ,) Kwargs represents the dictionary of the calling object, Kwargs={'name':'Egon', '  Age ': 'is the name of the child process

Method Description:

Property Description:

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, and set to True, p cannot create its own new process, must be set before P.start () P.name: Process name P.pid: Process Pidp.exitcode: The process is none at run time, if –n, indicates the end of signal N (understanding) P.authkey: The process's authentication key, which 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)

third, the use of the process class

Note: the process () in Windows must be placed in # if __name__ = = ' __main__ ': under

Since 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 uponImportbecause Windows does not have fork, the multi-processing module starts 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 that hides the internal call to process (), using the If__name__= = "__main__ ", the statement in this if statement will not be adjusted at the time of import
Detailed explanation

two ways to create and open a child process:

 fromMultiprocessingImportProcessImport TimedefWork (name):Print('task <%s> is runing'%name) Time.sleep (2)    Print('task <%s> is done'%name)if __name__=='__main__':    #Process (target=work,kwargs={' name ': ' Egon '})P1=process (target=work,args= ('Egon',)) P2=process (target=work,args= ('Alex',)) P1.start () P2.start ( )Print('Master')
Way One:
 fromMultiprocessingImportProcessImport Timeclassmyprocess (Process):def __init__(Self,name): Super ().__init__() Self.name=namedefRun (self):Print('task <%s> is runing'%self.name) Time.sleep (2)        Print('task <%s> is done'%self.name)if __name__=='__main__': P=myprocess ('Egon') P.start ()Print('Master')
Way Two:

Python 3 Programming Multi-process multiprocessing 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.