[OpenStack] The threading model in Nova

Source: Internet
Author: User
Tags epoll semaphore

1) Greenlet-python Libraries 1.1) What is a co-process (Coroutine)


Coroutine Wiki

Http://en.wikipedia.org/wiki/Coroutine

    • Coroutine is a re-segmentation on the basis of thread. Each process can contain multiple threads, each of which contains multiple coroutine
    • At any time, only one coroutine in the same thread can run.
    • The co-process is not real concurrency, and the co-process is just a different code switch within the same thread. Therefore, if the shared data is accessed between coroutine within the same thread, no resource protection is required.
1.2) coroutine implementation in Python-Greenletgreentlet is the Python coroutine implementation package. Usage can refer to the official example
From Greenlet import Greenletdef test1 ():    print    gr2.switch ()    print 34def test2 ():    print    Gr1.switch ()    Print 78gr1 = Greenlet (test1) GR2 = Greenlet (test2) Gr1.switch ()

The Greenlet.greenlet class is the Python implementation of coroutine, and the switch function can be called between different greenlet.
As can be seen from this code, Greenlet only implements the basic Coroutine function. If you want to use it directly in your project, we also want to
    • Integrate with Epoll to select the time to switch by selecting the IO
    • A general-purpose automatic switching/scheduling program
    • More Threads Advanced Concepts: Event, Semaphore, ThreadPool, etc.
So we need to use the Eventlet library.
2) Eventlet-The green thread Eventlet library based on the coprocessor is a multi-threaded IO framework under Python, eventlet through the encapsulation of Epoll and Greenlet, implementing the advanced concepts of IO multiplexing, thread pooling, etc. through Eventlet, we don't With the direct and Greenlet library to deal with.
Several classes commonly used in Eventlet
Greenthread: Green Thread. Greenthread is a subclass of the Eventlet class, so Greenthread is a co-process Coroutine
    • Sleep (): Signals to the scheduler to switch from the current greenthread to another greenthread
    • Spawn (): Start a new greenthread

Greenpool: Green thread pool. Contains 1 to n Greenthread.
    • Spawn (): Let Greenpool use the Greenthread in the pool to run the client-submitted task. If no greenthread is available in the pool, spawn () will block until the Greenthread is available

Event: Event Classes
    • Wait (): Let the greenthread of the call event.wait () be in a blocking wait state until the other Greenthread calls Event.send ()
    • Send (): Sends a signal to let all greenthread that are blocked on the event end the wait state and continue running.

Semaphore: Signal Volume
    • Acquire: Get semaphore. At the same time, only one greenthread can get semaphore, and Greenthread will be in a blocking wait state when semaphore cannot be obtained.
    • Release: Releasing the already acquired Semaphre

3) Nova Service-Greenthread-based business package Nova service is a further encapsulation of the Eventlet package
    • Each service contains a greenthread pool that runs the business code on the Greenthread.
    • Each service contains an event variable to wait for all greenthread to end






[OpenStack] The threading model in Nova

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.