Skip to main content

asyncio-gevent

asyncio-gevent makes asyncio and gevent compatible. It provides utilities for

  • running gevent on asyncio (by using asyncio as gevent's event loop, work in progress)
  • running asyncio on gevent (by using gevent as asyncio's event loop)
  • converting greenlets to asyncio futures
  • converting futures to asyncio greenlets

asyncio-gevent is a fork and complete rewrite of aiogevent and tulipcore for modern python 3.

Install

Install asyncio-gevent from pypi using your favourite package manager.

# If you use poetry
poetry add asyncio-gevent

# If you use pip
pip install asyncio-gevent

Usage

Running gevent on asyncio

In order to run gevent on asyncio, gevent needs to be initialised to use the asyncio event loop. This is done by setting the environment variable GEVENT_LOOP to asyncio_gevent.gevent_loop.GeventLoop and then starting python.

GEVENT_LOOP=asyncio_gevent.gevent_loop.GeventLoop python3 myscript.py

gevent will now run on asyncio.

Alternatively, you can also set the loop configuration setting, preferably right after importing gevent and before monkey patching.

import gevent
gevent.config.loop = "asyncio_gevent"

Running asyncio on gevent

In order to run asyncio on gevent, we need to set the (default) EventLoopPolicy to use asyncio_gevent.EventLoopPolicy.

import gevent.monkey
gevent.monkey.patch_all()

import asyncio

import asyncio_gevent

asyncio.set_default_event_loop_policy(asyncio_gevent.EventLoopPolicy)

Converting greenlets to asyncio futures

Use asyncio_gevent.wrap_greenlet to convert a greenlet to an asyncio future. The future yields once the greenlet has finished execution.

# The following assumes that the gevent/asyncio bindings have already been initialised
import gevent

import asyncio

import asyncio_gevent


def blocking_function() -> int:
    gevent.sleep(10)
    return 42


async def main() -> None:
    greenlet = gevent.spawn(blocking_function)
    future = asyncio_gevent.wrap_greenlet()
    result = await future


asyncio.run(main())

Converting asyncio futures to greenlets

Use asyncio_gevent.yield_future to convert a future to a greenlet.

# The following assumes that the gevent/asyncio bindings have already been initialised

import gevent

import asyncio

import asyncio_gevent


async def async_function() -> int:
    await asyncio.sleep(10)
    return 42


def main() -> None:
    future = async_function()
    greenlet = asyncio_gevent.yield_future(future)
    greenlet.join()

main()

License

MIT

Metadata

Release files for asyncio-gevent 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asyncio-gevent 0.1.1
File Size Uploaded
asyncio-gevent-0.1.1.tar.gz 14.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for asyncio-gevent 0.1.1
File Interpreter ABI Platform
asyncio_gevent-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 29.1 kB

Release files / asyncio-gevent-0.1.1.tar.gz

Download URL asyncio-gevent-0.1.1.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
672245bd44b6d9e0c4bc6e1e6b0d4d753f568ca3a2a6eff0cd4d2f18c6b2de4f
BLAKE2b-256 checksum
How to use checksums
7b5be835df7c0d11243722ab45e5bdc0bde62dce6ce71dfbdc4df2b0e14ca29d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.6 CPython/3.9.4 Linux/5.11.16-arch1-1

Release files / asyncio_gevent-0.1.1-py3-none-any.whl

Download URL asyncio_gevent-0.1.1-py3-none-any.whl
Size 15.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d5660de70c67d88fe8693d4592543e4a07950a9b0afbd2dc9265b1c560118bad
BLAKE2b-256 checksum
How to use checksums
8b6c79dbdda1b06dfd53f2da32fec1627012f275f7ce72fe7ec3a5e9b1628461
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.6 CPython/3.9.4 Linux/5.11.16-arch1-1

Release history Release notifications | RSS feed

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page