Coroutine and multithreading, multi-process

Source: Internet
Author: User

Multiple tasks can be performed using multi-threaded or multi-process.

Multiple processes, the same variable, each with a copy in each process, not affecting

In multi-threading, all variables are shared by all threads. And the switch between the threads is the system to dispatch, cannot control, so may

Multiple threads in a process may invoke the value of a variable at the same time, causing confusion in the value of the variable, when the thread lock is introduced, but the thread lock is prone to deadlock and prevents multithreading.

In addition, the Python interpreter because of the design of the Gil Global lock, resulting in multithreading can not take advantage of multicore. Multi-threaded concurrency is a beautiful dream in Python.

In thread and process, the process should be preferred because the process is more stable and the process can be distributed over multiple machines, while the thread is most

Can only be distributed to multiple CPUs on the same machine.

Multi-process consumes more resources, and the number of processes supported by the system is limited, although the running task is more stable

A single threaded thread crash can cause the entire process to crash, and threads a global lock.

Asynchronous IO Support, you can use a single-process single-threaded model to perform multitasking, a new model called the event-driven model.

The co-process is an asynchronous IO support mode. The process of this seed program can be interrupted at execution time, waiting for the time-consuming operation to notify the main thread after the message is sent,

Then proceed from where it was just interrupted. In this way, the switching between tasks is controlled by the internal definition of the program, and does not require a lock, and the execution efficiency is high.

Coroutine and multithreading, multi-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.