mirror of https://github.com/ekimekim/wubloader
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
708 B
Python
28 lines
708 B
Python
|
|
"""A group of greenlets running tasks.
|
|
Each task has a key. If a task with that key is already running,
|
|
it is not re-run."""
|
|
|
|
import gevent
|
|
|
|
|
|
class KeyedGroup:
|
|
def __init__(self):
|
|
self.greenlets = {}
|
|
|
|
def spawn(self, key, func, *args, **kwargs):
|
|
if key not in self.greenlets:
|
|
self.greenlets[key] = gevent.spawn(self._wrapper, key, func, *args, **kwargs)
|
|
return self.greenlets[key]
|
|
|
|
def wait(self):
|
|
"""Blocks until all tasks started before wait() was called are finished."""
|
|
gevent.wait(self.greenlets.values())
|
|
|
|
def _wrapper(self, key, func, *args, **kwargs):
|
|
try:
|
|
return func(*args, **kwargs)
|
|
finally:
|
|
assert self.greenlets[key] is gevent.getcurrent()
|
|
del self.greenlets[key]
|