guv.greenpool - greenthread pools

class guv.greenpool.GreenPool(size=1000)[source]

Bases: object

Pool of greenlets/GreenThreads

This class manages a pool of greenlets/GreenThreads

__init__(size=1000)[source]
Parameters:size – maximum number of active greenlets
free() → None[source]

Return the number of greenthreads available for use

If zero or less, the next call to spawn() or spawn_n() will block the calling greenthread until a slot becomes available.

resize(new_size) → None[source]

Change the max number of greenthreads doing work at any given time

If resize is called when there are more than new_size greenthreads already working on tasks, they will be allowed to complete but no new tasks will be allowed to get launched until enough greenthreads finish their tasks to drop the overall quantity below new_size. Until then, the return value of free() will be negative.

running() → None[source]

Return the number of greenthreads that are currently executing functions in the GreenPool

spawn(function, *args, **kwargs) → None[source]

Run the function with its arguments in its own green thread

Returns the GreenThread object that is running the function, which can be used to retrieve the results.

If the pool is currently at capacity, spawn will block until one of the running greenthreads completes its task and frees up a slot.

This function is reentrant; function can call spawn on the same pool without risk of deadlocking the whole thing.

spawn_n(function, *args, **kwargs) → None[source]

Create a greenthread to run the function like spawn(), but return None

The difference is that spawn_n() returns None; the results of function are not retrievable.

starmap(function, iterable) → None[source]

Apply each item in iterable to function

Each item in iterable must be an iterable itself, passed to the function as expanded positional arguments. This behaves the same way as itertools.starmap(), except that func is executed in a separate green thread for each item, with the concurrency limited by the pool’s size. In operation, starmap consumes a constant amount of memory, proportional to the size of the pool, and is thus suited for iterating over extremely long input lists.

waitall() → None[source]

Wait until all greenthreads in the pool are finished working

waiting() → None[source]

Return the number of greenthreads waiting to spawn.

class guv.greenpool.GreenPile(size_or_pool=1000)[source]

Bases: object

An abstraction representing a set of I/O-related tasks

Construct a GreenPile with an existing GreenPool object. The GreenPile will then use that pool’s concurrency as it processes its jobs. There can be many GreenPiles associated with a single GreenPool.

A GreenPile can also be constructed standalone, not associated with any GreenPool. To do this, construct it with an integer size parameter instead of a GreenPool.

It is not advisable to iterate over a GreenPile in a different greenlet than the one which is calling spawn. The iterator will exit early in that situation.

__init__(size_or_pool=1000)[source]
Parameters:size_or_pool (int or GreenPool) – either an existing GreenPool object, or the size a new one to create
next() → None[source]

Wait for the next result, suspending the current GreenThread until it is available

Raises StopIteration:
 when there are no more results.
spawn(func, *args, **kwargs) → None[source]

Run func in its own GreenThread

The Result is available by iterating over the GreenPile object.

Parameters:
  • func (Callable) – function to call
  • args – positional args to pass to func
  • kwargs – keyword args to pass to func