File: asyncio.py

package info (click to toggle)
python-eventlet 0.40.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,200 kB
  • sloc: python: 25,101; sh: 78; makefile: 31
file content (57 lines) | stat: -rw-r--r-- 1,723 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Asyncio compatibility functions.
"""
import asyncio

from greenlet import GreenletExit

from .greenthread import spawn, getcurrent
from .event import Event
from .hubs import get_hub
from .hubs.asyncio import Hub as AsyncioHub

__all__ = ["spawn_for_awaitable"]


def spawn_for_awaitable(coroutine):
    """
    Take a coroutine or some other object that can be awaited
    (``asyncio.Future``, ``asyncio.Task``), and turn it into a ``GreenThread``.

    Known limitations:

    * The coroutine/future/etc.  don't run in their own
      greenlet/``GreenThread``.
    * As a result, things like ``eventlet.Lock``
      won't work correctly inside ``async`` functions, thread ids aren't
      meaningful, and so on.
    """
    if not isinstance(get_hub(), AsyncioHub):
        raise RuntimeError(
            "This API only works with eventlet's asyncio hub. "
            + "To use it, set an EVENTLET_HUB=asyncio environment variable."
        )

    def _run():
        # Convert the coroutine/Future/Task we're wrapping into a Future.
        future = asyncio.ensure_future(coroutine, loop=asyncio.get_running_loop())

        # Ensure killing the GreenThread cancels the Future:
        def _got_result(gthread):
            try:
                gthread.wait()
            except GreenletExit:
                future.cancel()

        getcurrent().link(_got_result)

        # Wait until the Future has a result.
        has_result = Event()
        future.add_done_callback(lambda _: has_result.send(True))
        has_result.wait()
        # Return the result of the Future (or raise an exception if it had an
        # exception).
        return future.result()

    # Start a GreenThread:
    return spawn(_run)