Python Greenlet background introduction and implementation mechanism

Source: Internet
Author: User

Recently began to study Python's parallel development techniques, including multi-threading, multi-process, and so on. Gradually collated some of the information on the Internet, today to collate greenlet related information.

Technical background of concurrent processing

parallelization is now very important because, in many cases, parallel computing can greatly improve system throughput, especiallyin today's multi-core multiprocessor era, soThe old language, like Lisp, has been picked up again, and functional programming has become more popular. A library that describes the parallel processing of a python: Greenlet. Python has a very famous library called Stackless, used to do concurrent processing, mainly to get a micro-thread called Tasklet, and Greenlet and stackless the biggest difference is that he is very lightweight? Not enough, the biggest difference is that Greenlet needs you.to handle thread switching, which means that you need to specify which Greenlet to execute now and which green Let.

the implementation mechanism of Greenlet

previously, using Python to develop a Web program, you have been using FASTCGI mode. Multiple threads are then started in each process for request processingThere is a problem here is to ensure that each request response time is particularly short, or as long as more requestsa few slow times will cause the server to reject the service because no thread is able to respond to the request. Usually our service goes on the performance test, so the normal situation is not too big problem. But it is not possible to test all the scenarios. Once it appears, it will leave the user waiting for a long time without a response. Partially unavailable causes all to be unavailable. Later switched to Coroutine, Greenlet under Python. So a simple understanding of its implementation mechanism .
Each greenlet is just a Python object (Pygreenlet) in the heap. Sofor a process you create millions or even thousands of greenlet without problems.

typedef struct _GREENLET {pyobject_headchar* stack_start;char* stack_stop;char* stack_copy;intptr_t stack_saved; struct _greenlet* stack_prev;struct _greenlet* parent; pyobject* run_info;struct _frame* top_frame;int recursion_depth; pyobject* weakreflist; pyobject* Exc_type; pyobject* Exc_value; pyobject* Exc_traceback; Pyobject* Dict;} Pygreenlet;


each greenlet is actually a function, and saving this functionthe context at which execution occurs. For a function, the context is its stack. All greenlets of the same process share a common operating system-assigned user stack. So only the Greenlet with stack data at the same time can use this global stack. Greenlet is through Stack_stop,stack_ Start to save the stack bottom of its stack and the top of the stack, if there is a stack_stop of the greenlet that will be executed and the Greenlet overlap in the current stack, It is necessary to temporarily save the data from these overlapping greenlet stacks to the heap. The saved locations are recorded by Stack_copy and Stack_saved towhen recovering, copy the Stack_stop and Stack_start from the heap back to the stack. Otherwise, the stack data will be corrupted. So the applicationThese greenlet are created by continuously copying the data into the heap or copying it from the heap to the stack for concurrency. Using coroutine for IO-type applications is really comfortable.

here is a simple stack space model for Greenlet (from greenlet.c)
A pygreenlet is a range of C stacks addresses that must besaved and restored in such a It's the the full range of Thestack Contains valid data when we switch to it.     Stack layout for a greenlet: |               ^^^       |  |               Older Data |               |  | Stack_stop.        |_______________|      .               |        |      . |        Greenlet Data |      .   |        In Stack |    . * |_______________| .  .      _____________ stack_copy + stack_saved.               |     |             |        |      .     |     Data |        |greenlet data|      .   |     Unrelated |    |        Saved |      .      |     to |   | In Heap | Stack_start.     | This |. . |_____________|   stack_copy |               Greenlet |               |               |  |               Newer Data |     | VVV |


The following is a simple Greenlet code.

From Greenlet import Greenletdef test1 ():    print    gr2.switch ()    print 34def test2 ():    print    Gr1.switch ()    Print 78gr1 = Greenlet (test1) GR2 = Greenlet (test2) Gr1.switch ()

     The currently discussed process is generally supported by programming languages. At the moment I know that the languages available for the support include Python,lua,go,erlang, Scala, and Rust. the process is different from the thread is that the process is not the operating system to switch, but by the programmer code to switch, that is, the switch is controlled by the programmer, so that there is no thread so-called security issues.
all the threads share the entire process. so that the exchange between the process is also very convenient.
with respect to the second scenario (I/O multiplexing), the program that uses the co-write will be more direct , rather than splitting a complete process into multiple managed event handlers.
the disadvantage of the process may be that it cannot take advantage of multicore advantages, however, this can be solved by the way of the coprocessor + process.
the process can be used to handle concurrency to improve performance, or it can be used to implement state machines to simplify programming. I used more of it .The second one. Contact Python at the end of last year to learn about the concept of the Python process and laterby Pycon china2011access toprocessingyield, Greenlet is also a solution, and it seems to me to be a more usable one, especially to deal withStatusMachine.
at present , this piece has been basically completed, the back of the time to summarize.

To summarize:
1) Multi-process can take advantage of multicore advantages, but interprocess communication is more troublesome, in addition,ProcessThe increase in number will degrade performance and process switching costs are higher. Program process complexity is low relative to I/O multiplexing.
2) I/O multiplexing is the processing of multiple logical processes within a process, without process switching, high performance, and the sharing of information between processes is simple. However, the multi-core advantage cannot be exploited, and the program flow isprocessingcut into eachsmall pieces,The procedure is more complicated and difficult to understand.
3) thread runs inside a process, dispatched by the operating system, switchescostmoreLow, in addition, theythe virtual address space of a shared process is simple to share information between threads. But thread-safety issues lead to thread learning curvessteep, and easy toerror.
4) The process is provided by the programming language, and is controlled by the programmer, so there is noLineProcess Safetyproblem, you canused to handle state machines, concurrent requests, and so on. However, the multi-core advantage cannot be exploited.
the above four kinds of programs can be used together, I am more optimistic about isprocess + co-paththe mode.

Python Greenlet background introduction and implementation mechanism

Related Article

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.