| 1 |
.. _asyncio-compatibility: |
| 2 |
|
| 3 |
Asyncio compatibility in eventlet |
| 4 |
################################# |
| 5 |
|
| 6 |
It should be possible to: |
| 7 |
|
| 8 |
* Run eventlet and asyncio in the same thread. |
| 9 |
* Allow asyncio and eventlet to interact: eventlet code can use asyncio-based libraries, asyncio-based code can get results out of eventlet. |
| 10 |
|
| 11 |
If this works, it would allow migrating from eventlet to asyncio in a gradual manner both within and across projects: |
| 12 |
|
| 13 |
1. Within an OpenStack library, code could be a mixture of asyncio and eventlet code. |
| 14 |
This means migration doesn't have to be done in one stop, neither in libraries nor in the applications that depend on them. |
| 15 |
2. Even when an OpenStack library fully migrates to asyncio, it will still be usable by anything that is still running on eventlet. |
| 16 |
|
| 17 |
Prior art |
| 18 |
========= |
| 19 |
|
| 20 |
* Gevent has a similar model to eventlet. |
| 21 |
There exists an integration between gevent and asyncio that follows model proposed below: https://pypi.org/project/asyncio-gevent/ |
| 22 |
* Twisted can run on top of the asyncio event loop. |
| 23 |
Separately, it includes utilities for mapping its `Deferred` objects (similar to a JavaScript Promise) to the async/await model introduced in newer versions in Python 3, and in the opposite direction it added support for turning async/await functions into `Deferred`s. |
| 24 |
In an eventlet context, `GreenThread` would need a similar former of integration to Twisted's `Deferred`. |
| 25 |
|
| 26 |
Part 1: Implementing asyncio/eventlet interoperability |
| 27 |
====================================================== |
| 28 |
|
| 29 |
There are three different parts involved in integrating eventlet and asyncio for purposes |
| 30 |
|
| 31 |
1. Create a hub that runs on asyncio |
| 32 |
------------------------------------ |
| 33 |
|
| 34 |
Like many networking frameworks, eventlet has pluggable event loops, in this case called a "hub". Typically hubs wrap system APIs like `select()` and `epoll()`, but there also used to be a hub that ran on Twisted. |
| 35 |
Creating a hub that runs on top of the asyncio event loop should be fairly straightforward. |
| 36 |
|
| 37 |
Once this is done, eventlet and asyncio code can run in the same process and the same thread, but they would still have difficulties talking to each other. |
| 38 |
This latter requirement requires additional work, as covered by the next two items. |
| 39 |
|
| 40 |
2. Calling `async def` functions from eventlet |
| 41 |
---------------------------------------------- |
| 42 |
|
| 43 |
The goal is to allow something like this: |
| 44 |
|
| 45 |
.. code:: |
| 46 |
|
| 47 |
import aiohttp |
| 48 |
from eventlet_asyncio import future_to_greenlet # hypothetical API |
| 49 |
|
| 50 |
async def get_url_body(url): |
| 51 |
async with aiohttp.ClientSession() as session: |
| 52 |
async with session.get(url) as response: |
| 53 |
return await response.text() |
| 54 |
|
| 55 |
def eventlet_code(): |
| 56 |
green_thread = future_to_greenlet(get_url_body("https://example.com")) |
| 57 |
return green_thread.wait() |
| 58 |
|
| 59 |
The code would presumably be similar to https://github.com/gfmio/asyncio-gevent/blob/main/asyncio_gevent/future_to_greenlet.py |
| 60 |
|
| 61 |
3. Calling eventlet code from asyncio |
| 62 |
------------------------------------- |
| 63 |
|
| 64 |
The goal is to allow something like this: |
| 65 |
|
| 66 |
.. code:: |
| 67 |
|
| 68 |
from urllib.request import urlopen |
| 69 |
from eventlet import spawn |
| 70 |
from eventlet_asyncio import greenlet_to_future # hypothetical API |
| 71 |
|
| 72 |
def get_url_body(url): |
| 73 |
# Looks blocking, but actually isn't |
| 74 |
return urlopen(url).read() |
| 75 |
|
| 76 |
# This would likely be common pattern, so could be implemented as decorator... |
| 77 |
async def asyncio_code(): |
| 78 |
greenlet = eventlet.spawn(get_url_body, "https://example.com") |
| 79 |
future = greenlet_to_future(greenlet) |
| 80 |
return await future |
| 81 |
|
| 82 |
The code would presumably be similar to https://github.com/gfmio/asyncio-gevent/blob/main/asyncio_gevent/future_to_greenlet.py |
| 83 |
|
| 84 |
4. Limitations and potential unexpected behavior |
| 85 |
------------------------------------------------ |
| 86 |
|
| 87 |
``concurrent.futures.thread`` just uses normal threads, not Eventlet's special threads. |
| 88 |
Similarly, `asyncio.to_thread() <https://docs.python.org/3/library/asyncio-task.html#asyncio.to_thread>`_ |
| 89 |
specifically requires regular blocking code, it won't work correctly with Eventlet code. |
| 90 |
|
| 91 |
Multiple readers are not supported by the Asyncio hub. |
| 92 |
|
| 93 |
Part 2: How a port would work on a technical level |
| 94 |
================================================== |
| 95 |
|
| 96 |
Porting a library |
| 97 |
================= |
| 98 |
|
| 99 |
1. Usage of eventlet-based APIs would be replaced with usage of asyncio APIs. |
| 100 |
For example, `urllib` or `requests` might be replaced with `aiohttp <https://docs.aiohttp.org/en/stable/>`_. |
| 101 |
The interoperability above can be used to make sure this continues to work with eventlet-based APIs. |
| 102 |
|
| 103 |
The `awesome-asyncio <https://github.com/timofurrer/awesome-asyncio>`_ github repository propose a curated list of awesome |
| 104 |
Python asyncio frameworks, libraries, software and resources. Do not hesitate to take a look at it. You may find |
| 105 |
candidates compatible with asyncio that can allow you to replace some of your actual underlying libraries. |
| 106 |
2. Over time, APIs would need be migrated to be `async` function, but in the intermediate time frame a standard `def` can still be used, again using the interoperability layer above. |
| 107 |
3. Eventually all "blocking" APIs have been removed, at which point everything can be switched to `async def` and `await`, including external API, and the library will no longer depend on eventlet. |
| 108 |
|
| 109 |
Porting an application |
| 110 |
====================== |
| 111 |
|
| 112 |
An application would need to install the asyncio hub before kicking off eventlet. |
| 113 |
Beyond that porting would be the same as a library. |
| 114 |
|
| 115 |
Once all libraries are purely asyncio-based, eventlet usage can be removed and an asyncio loop run instead. |