Python asynchronous io\ database \ queue \ Cache

Source: Internet
Author: User

co-process

Co-process, also known as micro-threading, fiber. English name Coroutine. One sentence explains what a thread is: The process is a lightweight thread that is user-state .

The co-process has its own register context and stack. When the schedule is switched, the register context and stack are saved elsewhere, and the previously saved register context and stack are restored when it is cut back. So:

The process can retain the state of the last invocation (that is, a specific combination of all local states), each time the procedure is re-entered, which is equivalent to the state of the last call, in other words: The position of the logical stream at the last departure.

Benefits of the co-process:

    • No overhead for thread context switching
    • No need for atomic operation locking and synchronization overhead
    • Easy switching of control flow and simplified programming model
    • High concurrency + high scalability + Low cost: A CPU support for tens of thousands of processes is not a problem. Therefore, it is suitable for high concurrency processing.

Disadvantages:

    • Unable to take advantage of multicore resources: The nature of the process is a single thread, it can not be a single CPU at the same time multiple cores, the process needs and processes to run on multi-CPU. Of course, most of the applications that we write in the day-out are not necessary, except for CPU-intensive applications.
    • Blocking (Blocking) operations (such as IO) can block the entire program
Greenlet
1  fromGreenletImportGreenlet2 3 deftest1 ():4     Print(1)5 Gr2.switch ()6     Print(4)7 Gr2.switch ()8 deftest2 ():9     Print(2)Ten Gr3.switch () One     Print(5) A Gr3.switch () - deftest3 (): -     Print(3) the Gr1.switch () -     Print(6) -GR1 =Greenlet (test1) -GR2 =Greenlet (test2) +GR3 =Greenlet (TEST3) - Gr1.switch () +  A    
View Codegevent

Gevent is a third-party library that makes it easy to implement concurrent or asynchronous programming through Gevent, and the main pattern used in Gevent is Greenlet, which is a lightweight coprocessor that accesses Python in the form of a C extension module. Greenlet all run inside the main program operating system process, but they are dispatched in a collaborative manner.

1 Importgevent2 3 deftest1 ():4     Print(1)5Gevent.sleep (2)6     Print(4)7 deftest2 ():8     Print(2)9Gevent.sleep (1)Ten     Print(5) One deftest3 (): A     Print(3) - gevent.sleep (0) -     Print(6) the Gevent.joinall ([ - Gevent.spawn (test1), - Gevent.spawn (test2), - Gevent.spawn (test3), +])
View Code

Python asynchronous io\ database \ queue \ Cache

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.