Skip to main content

Asyncio integration with sync code using greenlets.

Project description

greenletio

Build Status

This project allows synchronous and asynchronous functions to be used together. Unlike other methods based on executors and thread or process pools, greenletio allows synchronous functions to work like their asynchronous counterparts, without the need to create expensive threads or processes.

Quick start

Installation

This package is installed with pip:

$ pip install greenletio

async_

The async_ function makes a synchronous function awaitable.

import asyncio
from greenletio import async_

def sync_function(arg):
    pass

async def async_function():
    await async_(sync_function)(42)

asyncio.run(async_function())

This function can also be used as a decorator:

import asyncio
from greenletio import async_

@async_
def sync_function(arg):
    pass

async def async_function():
    await sync_function(42)

asyncio.run(async_function())

await_

The await_ function can be used to await an asynchronous function in a synchronous one without blocking the asyncio loop:

from greenletio import await_

async def async_function():
    pass

def sync_function():
    await_(async_function())

Sometimes it is more convenient to use await_ as a decorator to make an asynchronous function callable from synchronous code (once again, without blocking the loop):

from greenletio import await_

@await_
async def async_function():
    pass

def sync_function():
    async_function()

Note that synchronous functions used in asynchronous applications must follow the rules that apply to asynchronous functions with regards to not calling any blocking code.

Why?

Porting an application to asyncio is in general very complicated, as it requires a large portion of the codebase to be converted due to the "virality" of asynchronous code, which requires that only an asynchronous function call another asynchronous function.

This package provides a solution to allow synchronous and asynchronous code to call each other without blocking the asynchronous loop.

How is this possible?

greenletio combines asynchronous functions with greenlets to achieve what is not possible using standalone Python.

Greenlets provide a way to context-switch or "jump" from the middle of a running function into another, and later resume the first at the place it was interrupted.

This opens the possibility for a synchronous function to "escape" a blocking wait by context-switching into an asynchronous function that releases the CPU back to the loop. The interrupted function would only be resumed once the blocking condition is resolved.

Previous work

The idea for greenletio originated in a proof-of-concept gist by Mike Bayer that used greenlets to prevent synchronous code from blocking. The intent was to use this technique to allow SQLAlchemy to work in asynchronous applications.

Since Mike's code became public we learned of another project combining coroutines and greenlets with the same goal called greenback, by Joshua Oreman.

The overall design of greenletio is based on eventlet.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

greenletio-0.0.3.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

greenletio-0.0.3-py2.py3-none-any.whl (7.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file greenletio-0.0.3.tar.gz.

File metadata

  • Download URL: greenletio-0.0.3.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

File hashes

Hashes for greenletio-0.0.3.tar.gz
Algorithm Hash digest
SHA256 eb4be0e5d967e9297b73f9ee926c023fdc62f4f96ecff5bfea2468f464958ce0
MD5 2bf59f136a738765593427acf292c5f1
BLAKE2b-256 94367954a153d2d4a1a41b678f5fb1f1762e33ca2abf051e33dc75e43dbc3455

See more details on using hashes here.

File details

Details for the file greenletio-0.0.3-py2.py3-none-any.whl.

File metadata

  • Download URL: greenletio-0.0.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

File hashes

Hashes for greenletio-0.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5646c8d41065700978d647deac56b7655f52894c84c8d5c339d9b7dc7412ed55
MD5 22d81a8260131c3fb76251e7260bd39b
BLAKE2b-256 6159f5326bb80d68c655a11f01045862d88453c90b1f0249b1d70ccc832550d3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page