diff options
Diffstat (limited to 'aiogreen.py')
-rw-r--r-- | aiogreen.py | 97 |
1 files changed, 47 insertions, 50 deletions
diff --git a/aiogreen.py b/aiogreen.py index 8912d43..a754507 100644 --- a/aiogreen.py +++ b/aiogreen.py @@ -282,58 +282,55 @@ class EventletEventLoop(asyncio.SelectorEventLoop): future.add_done_callback(done) return event.wait() + def wrap_greenthread(self, gt): + """Wrap an eventlet GreenThread, or a greenlet, into a Future object. -class EventletEventLoopPolicy(asyncio.DefaultEventLoopPolicy): - _loop_factory = EventletEventLoop - - -def wrap_greenthread(gt, loop=None): - """Wrap an eventlet GreenThread, or a greenlet, into a Future object. - - The Future object waits for the completion of a greenthread. The result - or the exception of the greenthread will be stored in the Future object. + The Future object waits for the completion of a greenthread. The result + or the exception of the greenthread will be stored in the Future object. - The greenthread must be wrapped before its execution starts. If the - greenthread is running or already finished, an exception is raised. + The greenthread must be wrapped before its execution starts. If the + greenthread is running or already finished, an exception is raised. - For greenlets, the run attribute must be set. - """ - if loop is None: - loop = asyncio.get_event_loop() - fut = asyncio.Future(loop=loop) - - if not isinstance(gt, greenlet.greenlet): - raise TypeError("greenthread or greenlet request, not %s" - % type(gt)) + For greenlets, the run attribute must be set. + """ + fut = asyncio.Future(loop=self) + + if not isinstance(gt, greenlet.greenlet): + raise TypeError("greenthread or greenlet request, not %s" + % type(gt)) + + if gt: + raise RuntimeError("wrap_greenthread: the greenthread is running") + if gt.dead: + raise RuntimeError("wrap_greenthread: the greenthread already finished") + + if isinstance(gt, eventlet.greenthread.GreenThread): + orig_main = gt.run + def wrap_func(*args, **kw): + try: + orig_main(*args, **kw) + except Exception as exc: + fut.set_exception(exc) + else: + result = gt.wait() + fut.set_result(result) + gt.run = wrap_func + else: + try: + orig_func = gt.run + except AttributeError: + raise RuntimeError("wrap_greenthread: the run attribute " + "of the greenlet is not set") + def wrap_func(*args, **kw): + try: + result = orig_func(*args, **kw) + except Exception as exc: + fut.set_exception(exc) + else: + fut.set_result(result) + gt.run = wrap_func + return fut - if gt: - raise RuntimeError("wrap_greenthread: the greenthread is running") - if gt.dead: - raise RuntimeError("wrap_greenthread: the greenthread already finished") - if isinstance(gt, eventlet.greenthread.GreenThread): - orig_main = gt.run - def wrap_func(*args, **kw): - try: - orig_main(*args, **kw) - except Exception as exc: - fut.set_exception(exc) - else: - result = gt.wait() - fut.set_result(result) - gt.run = wrap_func - else: - try: - orig_func = gt.run - except AttributeError: - raise RuntimeError("wrap_greenthread: the run attribute " - "of the greenlet is not set") - def wrap_func(*args, **kw): - try: - result = orig_func(*args, **kw) - except Exception as exc: - fut.set_exception(exc) - else: - fut.set_result(result) - gt.run = wrap_func - return fut +class EventletEventLoopPolicy(asyncio.DefaultEventLoopPolicy): + _loop_factory = EventletEventLoop |