Python--eventlet

Source: Internet
Author: User

The difference between a "green thread" common thread in the context of Eventlet:

1. Green threads have little overhead and do not retain the "green thread" as a normal thread, and each network connection corresponds to at least one "green thread";

2. Green threads require artificial settings that allow them to cross-control the CPU, rather than preemption. A green thread can share data structures without explicit mutex control, because only a green thread gives control of the other green threads to gain access to each other's shared structure.

Is the relationship between the Eventlet, hub, thread, and process:

 _______________________________________| Python Process | |_________________________________|| |  Python thread |  || |_____   ___________________|  ||  | | Hub | |  Pool |  |  ||  | |_____| |_____________|  |  ||          |  | |  Greenthread |  |  |  ||          |  | |_____________|  |  |  ||          | |_____________|  |  ||          |  | |  Greenthread |  |  |  ||          |  | |_____________|  |  |  ||          | |_____________|  |  ||          |  | |  Greenthread |  |  |  ||          |  | |_____________|  |  |  ||          |                   |  |  |  ||          |        |  ...        |  |  ||          | |___________________|  |  ||                                 |  |  || |_________________________________|                                       || ||_________________________________|| |  Python thread |  || |_________________________________| ||_________________________________|| |  Python thread |  || |_________________________________|                                       ||                 || ...                   ||_______________________________________|

The green thread is the thread concept, the green threads within the same thread are executed sequentially, and the green threads want to synchronize, requiring the developer to explicitly implant the CPU at the blocked code location, at which point the hub takes over to dispatch, looking for another scheduled green thread within the same thread. Note Green threads are concepts within threads and cannot be synchronized across threads.

Eventlet Basic API

First, Hatch Green thread

Eventlet.spawn (func, *args, **kw)

The function creates a green thread that uses the parameters *args and **kw call function func , and multiple hatching of the green thread executes the task in parallel. The function returns a greenthread. A Greenthread object that can be used to get the return value of the function func .

  

Eventlet.spawn_n (func, *args, **kw)

The function is similar to spawn (), except that it cannot get the return value or exception thrown when the function func execution completes. The function is executed more quickly

  

eventlet.spawn_after (seconds, Func, *args, **kw)

The function is the same as spawn (), equivalent to seconds seconds after the execution of Spawn (). You can call Greenthread.cancel () to exit hatching and block call function func on the return value of the function

Second, control the green thread

eventlet.sleep (seconds=0)

Suspends the current green thread, allowing other green threads to execute

  

class Eventlet. Greenpool

Control the concurrency of the green thread pool, you can control the degree of concurrency, to control the total amount of memory consumed by concurrency, or to limit the number of connections in one part of the code, etc.

  

class Eventlet. Greenpile

The Greenpile object represents the working block. The object is an iterator to which you can populate the work, allowing you to read the results from it later

  

class Eventlet. Queue

Easy to perform the basic components of data exchange between units for communication between green threads,

  

class Eventlet. Timeout

You can add a timeout to anything and throw an exception exceptionafter timeout seconds. When exception is ignored or none, the Timeout instance itself is thrown. A Timeout instance is a context manager, so you can use it in the WITH statement

Third, Patch function

eventlet.import_patched (ModuleName, *additional_modules, **kw_additional_modules)

The introduction of the standard library module after the green version, so that the subsequent code to execute in a non-blocking form, the required parameters are the name of the target module, specifically refer to the Import Green

  

Eventlet.monkey_patch (All=true, Os=false, Select=false, Socket=false, Thread=false, Time=false)

Patching the specified system module in the global, the patched module is "Green thread-friendly", the keyword parameter indicates which modules need to be patched, if all is true , then all modules will be patched and ignore the other parameters Otherwise, the specific module corresponding to the parameters to control the patch of the specified module. Most parameters for the same name with their own module patching, such as OS, time, select, but the socket parameter is true, if the SSL module also exists, will be patched both the socket module and the SSL module, similar to the thread parameter is true, will patch thread, Threading and Queue modules.

Monkey_patch () can be called multiple times , see monkeypatching the standard Library

Iv. Network Applications

eventlet.connect (addr, family=2, Bind=none)

Turn on client sockets

Parameters:

    • Addr – The address of the destination server, for TCP sockets, this parameter should be a (host, port) tuple
    • Family – socket family, optional, see socket Documentation
    • Bind – Local address of the binding, optional

Return:

The "green" socket object after the connection

Eventlet.listen (addr, family=2, backlog=50)

Create sockets that can be used for serve () or a custom accept () loop. Setting a socket's so_reuseaddr can reduce interruptions.

  Parameters:

    • Addr: The address to listen to, such as the socket for the TCP protocol, which is a (host, port) tuple.
    • Family: Socket family.
    • Backlog: The maximum number of queued connections, at least 1, is determined by the system.

  Return:

The "green" socket object in the listener.

Eventlet.wrap_ssl (sock, *a, **kw)

Turns a normal socket into an SSL socket, which is the same interface as the Ssl.wrap_socket (). You can use Pyopenssl, but you will ignore cert_reqs ,ssl_version ,CA when using Pyopenssl parameters such as _certs ,do_handshake_on_connect , and suppress_ragged_eofs .

It is recommended to use the Create mode to invoke the method, such as: Wrap_ssl (Connect (addr)) or Wrap_ssl (Listen (addr), server_side=true) . This does not occur when the "bare" socket listens for non-SSL sessions unexpectedly.

Return:

The "green" SSL object.

  

Eventlet.serve (sock, handle, concurrency=1000)

Running the server on a given socket, for each incoming client connection, calls the parameter handle in a separate green thread, the function handle accepts two parameters, one is the client's socket object, and the other is the client address:

def myhandle (Client_sock, client_addr):    print ("Client Connected", CLIENT_ADDR) Eventlet.serve (Eventlet.listen ( ' 127.0.0.1 ', 9999)), Myhandle)

function handle will close the client socket when it returns

serve () blocks the green thread of the call until the server shuts down before returning, and if a green thread is required to return immediately, a new green thread can be hatched for serve ()

any handle Thrown by an uncaught exception will be treated as an exception thrown by serve (), causing the server to terminate, so you need to figure out which exceptions the application throws. The return value of the handle is ignored.

Throw a stopserve exception to properly end Server–that's the only way to get the server () function to return rather than raise.

The parameter concurrency controls the concurrency level, which is the maximum number of green threads that process requests at any time, and when the server reaches that limit, it does not accept new connections until there is an existing completion.

class Eventlet. Stopserve

Exception class for proper exit of serve ()

Python--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.