All Projects → python-trio → async_generator

python-trio / async_generator

Licence: other
Making it easy to write async iterators in Python 3.5

Programming Languages

python
139335 projects - #7 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to async generator

polyfill-php81
This component provides functions unavailable in releases prior to PHP 8.1.
Stars: ✭ 618 (+610.34%)
Mutual labels:  polyfill
webpack2-polyfill-plugin
Insert polyfills (such as Promise) for Webpack 2
Stars: ✭ 18 (-79.31%)
Mutual labels:  polyfill
NetStandardPolyfills
Type and Reflection polyfill extension methods. .NET 3.5+ and .NET Standard 1.0+.
Stars: ✭ 22 (-74.71%)
Mutual labels:  polyfill
stack
🥭 nxpm-stack lets you generate a complete and opinionated full-stack application in a Nx Workspace, ready to extend and deploy!
Stars: ✭ 98 (+12.64%)
Mutual labels:  generators
Javascript-Interview-Preparation
A curated collection of javascript interview questions & solutions.
Stars: ✭ 163 (+87.36%)
Mutual labels:  polyfill
react-native-wasm
A polyfill to use WebAssembly in React Native.
Stars: ✭ 39 (-55.17%)
Mutual labels:  polyfill
sinator
Sinatra application generator
Stars: ✭ 19 (-78.16%)
Mutual labels:  generators
co-sh
Using ES6 Proxies & Generators to run shell commands
Stars: ✭ 24 (-72.41%)
Mutual labels:  generators
require-polyfill
Make `require` work in browsers, at runtime. No code bundling needed!
Stars: ✭ 37 (-57.47%)
Mutual labels:  polyfill
github-wc-polyfill
Ensure that all GitHub and GitLab scripts required for UXP and SeaMonkey are loaded correctly
Stars: ✭ 87 (+0%)
Mutual labels:  polyfill
mapgen
map generator stuff
Stars: ✭ 26 (-70.11%)
Mutual labels:  generators
Polyfill
An artifact repository to assist writing Gradle Plugins for Android build system.
Stars: ✭ 68 (-21.84%)
Mutual labels:  polyfill
object-keys
Object.keys shim
Stars: ✭ 41 (-52.87%)
Mutual labels:  polyfill
weakmap-polyfill
ECMAScript6 WeakMap polyfill
Stars: ✭ 25 (-71.26%)
Mutual labels:  polyfill
appHistory
A polyfill for the AppHistory proposal
Stars: ✭ 21 (-75.86%)
Mutual labels:  polyfill
iframe-worker
A tiny WebWorker polyfill for the file:// protocol
Stars: ✭ 23 (-73.56%)
Mutual labels:  polyfill
Pyxell
Multi-paradigm programming language compiled to C++, written in Python.
Stars: ✭ 42 (-51.72%)
Mutual labels:  generators
o9n
🖥 A screen.orientation ponyfill
Stars: ✭ 55 (-36.78%)
Mutual labels:  polyfill
hippie68.github.io
This is sleirsgoevy's PS4 jailbreak for firmware 7.02/7.5x, upgraded with additional features and more payloads.
Stars: ✭ 35 (-59.77%)
Mutual labels:  backports
whatsup
Reactive framework, simple, fast, easy to use!
Stars: ✭ 115 (+32.18%)
Mutual labels:  generators
Join chatroom Documentation Status Automated test status Automated test status (Windows) Test coverage

The async_generator library

Python 3.6 added async generators. (What's an async generator? Check out my 5-minute lightning talk demo from PyCon 2016.) Python 3.7 adds some more tools to make them usable, like contextlib.asynccontextmanager.

This library gives you all that back to Python 3.5.

For example, this code only works in Python 3.6+:

async def load_json_lines(stream_reader):
    async for line in stream_reader:
        yield json.loads(line)

But this code does the same thing, and works on Python 3.5+:

from async_generator import async_generator, yield_

@async_generator
async def load_json_lines(stream_reader):
    async for line in stream_reader:
        await yield_(json.loads(line))

Or in Python 3.7, you can write:

from contextlib import asynccontextmanager

@asynccontextmanager
async def background_server():
    async with trio.open_nursery() as nursery:
        value = await nursery.start(my_server)
        try:
            yield value
        finally:
            # Kill the server when the scope exits
            nursery.cancel_scope.cancel()

This is the same, but back to 3.5:

from async_generator import async_generator, yield_, asynccontextmanager

@asynccontextmanager
@async_generator
async def background_server():
    async with trio.open_nursery() as nursery:
        value = await nursery.start(my_server)
        try:
            await yield_(value)
        finally:
            # Kill the server when the scope exits
            nursery.cancel_scope.cancel()

(And if you're on 3.6, you can use @asynccontextmanager with native generators.)

Let's do this

How come some of those links talk about "trio"?

Trio is a new async concurrency library for Python that's obsessed with usability and correctness – we want to make it easy to get things right. The async_generator library is maintained by the Trio project as part of that mission, and because Trio uses async_generator internally.

You can use async_generator with any async library. It works great with asyncio, or Twisted, or whatever you like. (But we think Trio is pretty sweet.)

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].