Python multi-threading, multi-process programming.

Source: Internet
Author: User

is the smallest unit in the computer currently allocating resources to the application;

Process, which is the smallest unit running the application in the current computer;

In the actual system, in fact, the process is divided into processes to achieve, so the participation time slice rotation is the thread ;

However, the units of resources for managing applications and the units for task scheduling are processes. More like a logical concept.

A thread is a finer unit of process separation, and a context switch between threads is much faster than a context switch between processes .

  

Multi-process and multicore, this concept is very strange, because the process will not run directly on the core.

multi-threaded vs. multicore, involving a kernel thread that corresponds to a user thread .

Kernel thread (Kernel thread), usually corresponds to the core is one by one, a processing core corresponding to a kernel process.

The current computer, configuration is generally hyper-threading, that is, through hardware redundancy, a physical core is simulated into two logical processing core ,

Corresponds to two kernel threads. So the CPU seen in the operating system is twice times the actual physical core.

such as dual core four thread, quad core eight thread

------------------------------------------------------------------------

Intel's CPU in order to achieve branch prediction technology, typically each core of the operating unit is double,-------Hyper-Threading

The operational unit of the physical core of the server, possibly more, supports larger Hyper-threading

------------------------------------------------------------------------

The corresponding relationship between the user thread and the kernel thread:

1) One by one corresponds, because the kernel thread is very few, so the number of user threads is limited;

2) Many to one, the user thread is very slow, the impact of scheduling;

3) Many-to-many, the current application of the model,

  

The multi-process library in Python, multiprocessing, provides methods and properties:

Property: Authkey,daemon (after the parent process terminates, the child process is automatically ended and must be set before start), Exitcode,name,pid

Method: Process (Target,name,args,kwargs), creating the Processes object

Is_live (), Process state

Join (' timeout '), which represents the end of the process waiting for the background, if timeout is specified, after a certain time, kill the process

Run (),

Start (),

Terminate (),

Join (), and so on all the background sub-processes execute well;

Cpu_count (), returns the number of CPUs currently owned, after Hyper-threading.

When a process is created, target can be specified as a function, or a process class can be instantiated directly.

To create a function process:

    

After you create the class process, start () automatically calls the run () function:

    

Add Daemon Property: The program will not wait until the worker's process is finished.

    

Multi-process traversal: Active_children ()

    

You can also join Lock,semaphore to achieve control of shared resources, event, and interprocess communication.

There are process queues (queue), and process pipelines (pipe)

Pool, process pools, do not need to manage the number of processes themselves, the process pool is automatically managed.

If the number of processes in the pool reaches the threshold, other processes are blocked.

Reference Blog

Https://www.cnblogs.com/kaituorensheng/p/4445418.html

Python multi-threading, multi-process programming.

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.