OpenStack co-concurrency Eventlet

Source: Internet
Author: User

Today listen to easystack a buddy talk Nova Synergy concurrency, combined with their previous understanding. Review the OpenStack Eventlet.

OpenStack, as a popular open-source cloud platform, has its own code to support high concurrency.

Let's start with Python concurrency, where concurrency in Python is: process, thread, and co-coroutines.

Process and thread everyone is more clear, that what is the association process, the introduction of the following:

    • Co-process and thread comparison, each of which has its own private stack and local variables
    • There can be a number of threads within a thread
    • Multiple threads can run concurrently. But within a thread, there is only one co-process running at a time, and there is no need to lock some shared variables
    • The order of the process is controlled by the program completely.
    • Unless you give up execution, until the end of execution
    • The process is a concept, the operating system is visible, implemented in multiple languages, such as go, Ruby, etc. Eventlet is one of the implementations in Python

The relationships between processes, threads, and sessions can be represented by:

Eventlet is the encapsulation of Greenlet, first understand the next Greenlet, here is an official given example:

1  fromGreenletImportGreenlet2 3 deftest1 ():4     Print125 Gr2.switch ()6     Print347 8 deftest2 ():9     Print56Ten Gr1.switch () One     Print78 A  -GR1 =Greenlet (test1) -GR2 =Greenlet (test2) theGr1.switch ()

The execution results are:

125634

The program is simple, define two threads, the last line G1.switch () jumps to test1 (), it prints 12, then jumps to test2 (), prints 56, then jumps back test1 (), prints 34, then test1 () ends, Gr1 dies, Back to Parent Greenlet, no more switching to test2, so 78 is not printed. In the example above, main Greenlet is their parent greenlet.

Although Greenlet is simple to use, it requires programmers to explicitly write code to switch between different threads, which is obviously unrealistic for large projects like OpenStack.

So Eventlet encapsulated the Greenlet, the following is the introduction of Eventlet:

    • High-performance network library by Second Life Open source
    • Based on Greenlet
    • Using patch standard module to implement the co-process
    • For the Greenlet package, provides greenthread
    • Internal use of timer to implement Greenlet Scheduler
    • Internal main loop loops through minimum heap lookups should be performed by the Greenlet

Python 2.x native is not supported by the Eventlet, and is implemented by a patch implementation of the standard library, such as in a Nova project nova/cmd/__init__.py

Import Eventlet  from Import Debugger if debugger.enabled ():     # turn off thread patching to enable the remote debugger    Eventlet.monkey_patch (Os=false, thread=False)else:    eventlet.monkey_patch (OS =false)

where Eventlet.monkey_patch () is a patch to the standard library.

When debugging the Nova code, it is sometimes possible to change the Eventlet.monkey_patch (os=false) inside the Else statement to Eventlet.monkey_patch (Os=false, thread= False)

In Eventlet's documentation, there are several interfaces for Eventlet, mainly including the following:

(1) Spawn (func, *args, **kw)
Start a greenthread to invoke the Func function, both args and KW are arguments passed to func. The return value is Greenthread. Greenthread Object

(2) Spawn_n (func, *args, **kw)
In addition to no return value, the others are the same as spawn

(3) Spawn_after (seconds, Func, *args, **kw)
Start after seconds seconds

(4) Sleep (seconds=0)
Suspend current Greenthread, give other greenthread the opportunity to execute

Participation Information:

http://greenlet.readthedocs.org/en/latest/

Http://eventlet.net/doc/basic_usage.html

http://blog.csdn.net/hackerain/article/details/7836993

Http://www.choudan.net/2013/08/18/OpenStack-eventlet%E5%88%86%E6%9E%90 (%e4%b8%80). html

OpenStack co-concurrency Eventlet

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.