The Eventlet.greenpool of Python learning

Source: Internet
Author: User
Tags imap iterable

This module provides support for the Greenthread pool.

The Greenthread pool provides a number of standby greenthread, effectively limiting the amount of memory that is caused by hatching greenthread too much, and when there is not enough free greenthread in the pool, the incubation process is suspended only if the previous work Greenthread complete the current work to prepare for the next task.

This module consists of two classes:

1. Eventlet.greenpool.GreenPool

2. Eventlet.greenpool.GreenPile

First, class Eventlet.greenpool.GreenPool (size=1000)

The object of this class is the pool of green threads, with a default capacity of 1000 green threads.

The method of the class

1. Free ()

2. IMAP (function, *iterables)

3. Resize (new_size)

4. Running ()

5. Spawn (function, *args, **kwargs)

6. Spawn_n (function, *args, **kwargs)

7. Starmap (function, iterable)

8. WaitAll ()

9. Waiting ()

1.

Free ()

Returns the greenthreads available in the current object.

If 0 or less, then spawn () and Spawn_n () will block calls to Greenthread until a new greenthread is available.

As to why a negative value might be returned here, see 3. Resize ()

2.

IMAP (function, *iterables)

The effect is equivalent to Itertools.imap (), which equates to Starmap () in both concurrency and memory usage.

For example, it can be very convenient to do something about a file:

def Worker (line):    return do_something [line] pool = Greenpool ()in Pool.imap (worker, open (" filename'r'print (result)          

3.

Resize (new_size)

Change the maximum number of greenthreads currently allowed to work concurrently

If there are currently more than new_size greenthreads in the work, they can do their own execution, but at this time no new greenthreads is allowed to be assigned. The new greenthreads can only be assigned if a sufficient number of greenthreads have completed their work and then the total number of greenthreads in the work is lower than new_size . Until then, the return value of Free() will be negative.

4.

Running ()

Returns the greenthreads in the current pool that is performing the task.

5.

Spawn (function, *args, **kwargs)
Incubate a usable greenthread from the current pool, perform function in this greenthread, parameter *args, **kwargs to function Parameters. Returns a Greenthread object that performs a function that can get the return value of a function from the greenthread object 。
If there is no spare greenthread in the current pool, the method blocks until a new available Greenthreads is released.
The function can be reused , and function can call the spawn method of the same Greenpool object without worrying about deadlocks.
6.
Spawn_n (function, *args, **kwargs)
  
Create a greenthread to run the function with the same effect as spawn (). only This function returns None, that is , the return value of function is discarded.
7.
Starmap (function, iterable)
  
Equivalent to itertools.starmap (), in addition to each element in an iterative object, a func is executed inside a greenthread. The upper limit of concurrency is limited by the capacity of the pool. In actual operation, the memory consumed by Starmap () is proportional to the capacity of the pool, making it particularly suitable for traversing a particularly long input list.
  
8.
WaitAll ()
  
Wait for all greenthreads in the pool to complete the work.
  
9.
waiting () /span>
Returns the number of  greenthreads currently waiting to hatch.
Second,
class Eventlet.greenpool.GreenPile (size_or_ pool=1000)   
Greenpile is an abstraction of some I/O-related tasks. The
can construct a  greenpile using an already existing  greenpool object that will use the concurrency of that pool when dealing with its own tasks. A  greenpool can correspond to multiple  greenpile.
A  greenpile can also be constructed independently and not bound to any  greenpool. You only need to pass in an integer as an argument when you create the instance.
using a greenthread iteration that is not currently called spawn () is not a wise  greenpile, and the iterator exits prematurely.  
methods for this class are
1. Next ()

Waits for the next result, suspends the current greenthread until the result is available. Throws a stopiteration exception when there are no more results.

2. Spawn (func, *args, **kw)

The Func is run in its own greenthread , and the results are stored in the Greenpile object, which you can iterate over to get those results.

The Eventlet.greenpool of Python learning

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.