gevent.pool – Managing greenlets in a group#
- class Group(*args)#
Maintain a group of greenlets that are still running, without limiting their number.
Links to each item and removes it upon notification.
Groups can be iterated to discover what greenlets they are tracking, they can be tested to see if they contain a greenlet, and they know the number (len) of greenlets they are tracking. If they are not tracking any greenlets, they are False in a boolean context.
Answer how many greenlets we are tracking. Note that if we are empty, we are False in a boolean context.
Answer if we are tracking the given greenlet.
Begin tracking the greenlet.
If this group is
full(), then this method may block until it is possible to track the greenlet.
Typically the greenlet should not be started when it is added because if this object blocks in this method, then the greenlet may run to completion before it is tracked.
- apply(func, args=None, kwds=None)#
Rough quivalent of the
apply()builtin function blocking until the result is ready and returning it.
funcwill usually, but not always, be run in a way that allows the current greenlet to switch out (for example, in a new greenlet or thread, depending on implementation). But if the current greenlet or thread is already one that was spawned by this pool, the pool may choose to immediately run the
funcraises will be propagated to the caller of
apply(that is, this method will raise the exception that
- apply_async(func, args=None, kwds=None, callback=None)#
When the returned greenlet gets to run, it will call
apply(), passing in func, args and kwds.
If callback is specified, then it should be a callable which accepts a single argument. When the result becomes ready callback is applied to it (unless the call failed).
This method will never block, even if this group is full (that is, even if
spawn()would block, this method will not).
The returned greenlet may or may not be tracked as part of this group, so
joiningthis group is not a reliable way to wait for the results to be available or for the returned greenlet to run; instead, join the returned greenlet.
- apply_cb(func, args=None, kwds=None, callback=None)#
apply()the given func(*args, **kwds), and, if a callback is given, run it with the results of func (unless an exception was raised.)
The callback may be called synchronously or asynchronously. If called asynchronously, it will not be tracked by this group. (
Poolcall it asynchronously in a new greenlet;
ThreadPoolcalls it synchronously in the current greenlet.)
Stop tracking the greenlet.
Return a value indicating whether this group can track more greenlets.
In this implementation, because there are no limits on the number of tracked greenlets, this will always return a
- imap(func, *iterables, maxsize=None) iterable #
An equivalent of
itertools.imap(), operating in parallel. The func is applied to each element yielded from each iterable in iterables in turn, collecting the result.
If this object has a bound on the number of active greenlets it can contain (such as
Pool), then at most that number of tasks will operate in parallel.
maxsize (int) –
If given and not-None, specifies the maximum number of finished results that will be allowed to accumulate awaiting the reader; more than that number of results will cause map function greenlets to begin to block. This is most useful if there is a great disparity in the speed of the mapping code and the consumer and the results consume a great deal of resources.
This is separate from any bound on the number of active parallel tasks, though they may have some interaction (for example, limiting the number of parallel tasks to the smallest bound).
Using a bound is slightly more computationally expensive than not using a bound.
imap_unordered()method makes much better use of this parameter. Some additional, unspecified, number of objects may be required to be kept in memory to maintain order by this function.
An iterable object.
Changed in version 1.1b3: Added the maxsize keyword parameter.
Changed in version 1.1a1: Accept multiple iterables to iterate in parallel.
- imap_unordered(func, *iterables, maxsize=None) iterable #
The same as
imap()except that the ordering of the results from the returned iterator should be considered in arbitrary order.
This is lighter weight than
imap()and should be preferred if order doesn’t matter.
imap()for more details.
- join(timeout=None, raise_error=False)#
Wait for this group to become empty at least once.
If there are no greenlets in the group, returns immediately.
By the time the waiting code (the caller of this method) regains control, a greenlet may have been added to this group, and so this object may no longer be empty. (That is,
group.join(); assert len(group) == 0is not guaranteed to hold.) This method only guarantees that the group reached a
lenof 0 at some point.
raise_error (bool) – If True (not the default), if any greenlet that finished while the join was in progress raised an exception, that exception will be raised to the caller of this method. If multiple greenlets raised exceptions, which one gets re-raised is not determined. Only greenlets currently in the group when this method is called are guaranteed to be checked for exceptions.
- Return bool:
A value indicating whether this group became empty. If the timeout is specified and the group did not become empty during that timeout, then this will be a false value. Otherwise it will be a true value.
Changed in version 1.2a1: Add the return value.
- kill(exception=<class 'greenlet.GreenletExit'>, block=True, timeout=None)#
Kill all greenlets being tracked by this group.
- killone(greenlet, exception=<class 'greenlet.GreenletExit'>, block=True, timeout=None)#
If the given greenlet is running and being tracked by this group, kill it.
- map(func, iterable)#
Return a list made by applying the func to each element of the iterable.
- map_async(func, iterable, callback=None)#
A variant of the map() method which returns a Greenlet object that is executing the map function.
If callback is specified then it should be a callable which accepts a single argument.
- spawn(*args, **kwargs)#
Begin a new greenlet with the given arguments (which are passed to the greenlet constructor) and add it to the collection of greenlets this group is monitoring.
The newly started greenlet.
Add the unstarted greenlet to the collection of greenlets this group is monitoring, and then start it.
- class PoolFull#
Raised when a Pool is full and an attempt was made to add a new greenlet to it in non-blocking mode.
- class Pool(size=None, greenlet_class=None)#
Create a new pool.
A pool is like a group, but the maximum number of members is governed by the size parameter.
size (int) –
If given, this non-negative integer is the maximum count of active greenlets that will be allowed in this pool. A few values have special significance:
None(the default) places no limit on the number of greenlets. This is useful when you want to track, but not limit, greenlets. In general, a
Groupmay be a more efficient way to achieve the same effect, but some things need the additional abilities of this class (one example being the spawn parameter of
gevent.baseserver.BaseServerand its subclass
0creates a pool that can never have any active greenlets. Attempting to spawn in this pool will block forever. This is only useful if an application uses
wait_available()with a timeout and checks
free_count()before attempting to spawn.
- add(greenlet, blocking=True, timeout=None)#
Begin tracking the given unstarted greenlet, possibly blocking until space is available.
Usually you should call
start()to track and start the greenlet instead of using this lower-level method, or
spawn()to also create the greenlet.
blocking (bool) – If True (the default), this function will block until the pool has space or a timeout occurs. If False, this function will immediately raise a Timeout if the pool is currently full.
timeout (float) – The maximum number of seconds this method will block, if
blockingis True. (Ignored if
PoolFull – if either
blockingis False and the pool was full, or if
blockingis True and
If the greenlet has already been started and blocking is true, then the greenlet may run to completion while the current greenlet blocks waiting to track it. This would enable higher concurrency than desired.
Changed in version 1.3.0: Added the
Return a number indicating approximately how many more members can be added to this pool.
Return a boolean indicating whether this pool is full, e.g. if
False if there is room for new members, True if there isn’t.
- start(greenlet, blocking=True, timeout=None) None #
Add the unstarted greenlet to the collection of greenlets this group is monitoring and then start it.
Parameters are as for
Wait until it’s possible to spawn a greenlet in this pool.
timeout (float) – If given, only wait the specified number of seconds.
If the pool was initialized with a size of 0, this method will block forever unless a timeout is given.
A number indicating how many new greenlets can be put into the pool without blocking.
Changed in version 1.1a3: Added the