All Projects → miguelgrinberg → Greenletio

miguelgrinberg / Greenletio

Licence: mit
Asyncio integration with sync code using greenlets.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Greenletio

Aiohttp admin
admin interface for aiohttp application http://aiohttp-admin.readthedocs.io
Stars: ✭ 207 (+102.94%)
Mutual labels:  asyncio, async-await
kbio
Another Async IO Framework based on io_uring
Stars: ✭ 54 (-47.06%)
Mutual labels:  asyncio, async-await
tomodachi
💻 Microservice library / framework using Python's asyncio event loop with full support for HTTP + WebSockets, AWS SNS+SQS, RabbitMQ / AMQP, middleware, etc. Extendable for GraphQL, protobuf, gRPC, among other technologies.
Stars: ✭ 170 (+66.67%)
Mutual labels:  asyncio, async-await
Aiozipkin
Distributed tracing instrumentation for asyncio with zipkin
Stars: ✭ 161 (+57.84%)
Mutual labels:  asyncio, async-await
Async Reduce
Reducer for similar simultaneously coroutines
Stars: ✭ 17 (-83.33%)
Mutual labels:  asyncio, async-await
Aiomisc
aiomisc - miscellaneous utils for asyncio
Stars: ✭ 200 (+96.08%)
Mutual labels:  asyncio, async-await
nardis
A small web framework based on ASGI
Stars: ✭ 14 (-86.27%)
Mutual labels:  asyncio, async-await
aioflask
Flask running on asyncio!
Stars: ✭ 192 (+88.24%)
Mutual labels:  asyncio, async-await
Aiomonitor
aiomonitor is module that adds monitor and python REPL capabilities for asyncio application
Stars: ✭ 430 (+321.57%)
Mutual labels:  asyncio, async-await
Anyio
High level compatibility layer for multiple asynchronous event loop implementations on Python
Stars: ✭ 343 (+236.27%)
Mutual labels:  asyncio, async-await
Cppcoro
A library of C++ coroutine abstractions for the coroutines TS
Stars: ✭ 2,118 (+1976.47%)
Mutual labels:  asyncio, async-await
Uvloop
Ultra fast asyncio event loop.
Stars: ✭ 8,246 (+7984.31%)
Mutual labels:  asyncio, async-await
Aiormq
Pure python AMQP 0.9.1 asynchronous client library
Stars: ✭ 112 (+9.8%)
Mutual labels:  asyncio, async-await
Aioodbc
aioodbc - is a library for accessing a ODBC databases from the asyncio
Stars: ✭ 206 (+101.96%)
Mutual labels:  asyncio, async-await
Riprova
Versatile async-friendly library to retry failed operations with configurable backoff strategies
Stars: ✭ 106 (+3.92%)
Mutual labels:  asyncio, async-await
Sqlalchemy aio
Asyncio strategy for SQLAlchemy.
Stars: ✭ 299 (+193.14%)
Mutual labels:  asyncio, async-await
Asyncio
asyncio historical repository
Stars: ✭ 952 (+833.33%)
Mutual labels:  asyncio, async-await
Web Applications With Fastapi Course
Demo code and other handouts for students of our FastAPI Web Apps course.
Stars: ✭ 56 (-45.1%)
Mutual labels:  asyncio, async-await
Jd4
Judging daemon for programming contests
Stars: ✭ 85 (-16.67%)
Mutual labels:  asyncio
Rororo
Implement aiohttp.web OpenAPI 3 server applications with schema first approach.
Stars: ✭ 95 (-6.86%)
Mutual labels:  asyncio

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.

spawn

The spawn function launches a synchronous Python function asynchronously as a greenlet. The new greenlet (and any function called from it) can use the await_ function.

green.*

The modules under greenletio.green are drop-in replacements of the Python standard library modules of the same name, implemented using the async_, await_ and spawn primitives.

The goal is to provide replacements for all the blocking functions in the standard library, so that code written as blocking can be used asynchronously.

Currently implemented modules are socket, ssl, threading, and time.

patch_blocking

The patch_blocking context manager can be used to import code written for the Python standard library with all the blocking functions redirected to their green.* replacements.

patch_psycopg2

The patch_psycopg2 function configures psycopg2 to access Postgres database in non-blocking mode.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].