Python's co-process

Source: Internet
Author: User

    • Co-process, also known as micro-threading, is a lightweight thread of 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 when you cut back, restore the previous
      The saved register context and stack. Therefore, 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-loaded,
      is equivalent to entering the state of the last call, in other words: The position of the logical stream at the time of the last departure.
    • Benefits of the co-process:
      • Without the overhead of thread context switching;
      • No need for atomic operation locking and synchronization overhead;
      • Easy to switch the control flow, simplify the programming model;
      • High concurrency + high scalability + Low cost: A CPU support for tens of thousands of processes is not a problem, so it is suitable for high concurrency processing.
    • Disadvantages:
      • Cannot take advantage of multi-core resources: The nature of the process is a single thread, it cannot simultaneously combine multiple cores of a single CPU, and the process needs and processes can be run in multiple
        CPU, of course, most of the applications we write on a daily basis are not necessary, except for CPU-intensive applications;
      • Blocking (Blocking) operations (such as IO) can block the entire program;
# Example: Yield support for the process import timeimport queuedef consumer (name): Print ('---start eating buns---') while True:new_baozi = Yi     Eld print (' [%s] eating bun%s '% (name, New_baozi)) def producer (): R = con.__next__ () R = con2.__next__ () n = 0 While n < 5:n + = 1 con.send (n) con2.send (n) print (' \033[32;1m[producer]\033[0m is making buns% S '% n) if __name__ = = ' __main__ ': con = consumer (' C1 ') # Create a generator object con con2 = consumer (' C2 ') # Create another generator Object Con2 p = producer () # example two: Greenlet under the process from Greenlet import greenletdef test1 (): Print (' two ') Gr2.switch () p        Rint (' Gr2.switch ') def test2 (): Print (' ') Gr1.switch () print (' + ') Gr1 = Greenlet (test1) # print (GR1) # <greenlet.greenlet Object at 0x10877f930>gr2 = Greenlet (test2) Gr1.switch () # example three: The Gevent is a third-party library that can It is easy to implement concurrent or asynchronous programming through Gevent, and the primary mode used in Gevent is greenlet,#, which is a lightweight coprocessor that accesses Python in the form of a C extension module.   Import geventdef foo (): Print (' Running in foo ') Gevent.sleep (1) Print (' Explicit context switch to Foo again. ') def bar (): Print (' Explicit context to bar ') Gevent.sleep (1) Print (' Implicit context switch back to bar. ') Gevent.joinall ([Gevent.spawn (foo), Gevent.spawn (bar),])


Resources:

    • Python Full Stack

Python's co-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.