Concurrent programming Multi-process process process

Source: Internet
Author: User
Tags terminates

Python concurrent Programming Multi-process 1.1 multiprocessing module

Multithreading in Python cannot take advantage of multicore resources, and if you want to fully use the resources of multicore CPUs, most of the situations in Python require multiple processes. Python provides us with a very useful multi-process package multiprocessing!

OS.CPU_COUTN ()

The multiprocessing module is used to open sub-processes and perform our customized tasks (such as functions, implementation functions, etc.) in the module.

The multiprocessing module has many functions:

?支持子进程?通信和共享数据?执行不同形式的同步?提供了 Process,Queue,Pipe,Lock 等组件

Unlike threads, processes do not have any shared state, and process-modified data is restricted to the city.

Introduction to the 1.2 process class to create a class for processes
Process([group [, target [, name [, args [, kwargs]]]]]),由该类实例化得到的对象,表示一个子进程中的任务(尚未启动)

Emphasis: 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 introduction
    1. The group parameter is not used, and the value is always None (not understood, does not need to understand)
    2. Target represents the calling object, the task to be performed by the child process.
    3. Args represents the positional parameter tuple of the calling object, args = ("A ', ' a ', ' B ', ' C ': ' FF ')
    4. Kwargs represents the dictionary of the calling object Kwargs = {' A ': 1, ' B ': 2, ' C ': 3}
    5. Name is the child process
Method Introduction
    1. P.start () Start: Starts the process and calls the P.run () in the child process
    2. P.run () Call: The method that runs at the start of a process, is exactly what it calls the function specified by target, and we must implement the method in the class of our custom class.
    3. P.terminate () terminate the process: Force terminate the process p, no cleanup operation, if p creates a child process, the child process is a zombie process, using this method requires special care in this case. If P also holds a lock then it will not be released, resulting in deadlocks.
    4. P.is_alive (): Returns True if P is still running.
    5. P.join ([timeout]) waits for termination: The main thread waits for p to terminate (emphasizing that the main thread is in the same state, and P is in the running state). Timeout is an optional time-out, and it needs to be emphasized that P.join can only join the start-open process, not Jo In the process that the run opens.
Properties Introduction
    1. 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 set to True, p cannot create its own new process and must be set before P.start ()
    2. P.name: The name of the process
    3. P.pid: PID of the process
    4. P.exitcode: The process is None at run time, and if-n means the end of signal N (understanding part).
    5. P.authkey: The authentication key for the process, by default, is a 32-character string that is 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 (see section).
Use of the 1.3 Process class

First step: Two ways to create and open a child process

Under Windows Process () must be placed under ifname = = 'mian: '

#开进程的方法详见我的 pycharm??

Concurrent programming Multi-process process process

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.