Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Making it easy to write async iterators in Python 3.5

License

Unknown and 2 other licenses found

Licenses found

Unknown
LICENSE
Apache-2.0
LICENSE.APACHE2
MIT
LICENSE.MIT
NotificationsYou must be signed in to change notification settings

python-trio/async_generator

Join chatroomDocumentation StatusAutomated test statusAutomated test status (Windows)Test coverage

The async_generator library

Python 3.6 addedasync generators. (What's an asyncgenerator?Check out my 5-minute lightning talk demo from PyCon 2016.) Python 3.7 adds some moretools to make them usable, likecontextlib.asynccontextmanager.

This library gives you all that back to Python 3.5.

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

asyncdefload_json_lines(stream_reader):asyncforlineinstream_reader:yieldjson.loads(line)

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

fromasync_generatorimportasync_generator,yield_@async_generatorasyncdefload_json_lines(stream_reader):asyncforlineinstream_reader:awaityield_(json.loads(line))

Or in Python 3.7, you can write:

fromcontextlibimportasynccontextmanager@asynccontextmanagerasyncdefbackground_server():asyncwithtrio.open_nursery()asnursery:value=awaitnursery.start(my_server)try:yieldvaluefinally:# Kill the server when the scope exitsnursery.cancel_scope.cancel()

This is the same, but back to 3.5:

fromasync_generatorimportasync_generator,yield_,asynccontextmanager@asynccontextmanager@async_generatorasyncdefbackground_server():asyncwithtrio.open_nursery()asnursery:value=awaitnursery.start(my_server)try:awaityield_(value)finally:# Kill the server when the scope exitsnursery.cancel_scope.cancel()

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

Let's do this

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

Trio is a new async concurrencylibrary for Python that's obsessed with usability and correctness – wewant to make iteasy to get thingsright. Theasync_generatorlibrary is maintained by the Trio project as part of that mission, andbecause Trio usesasync_generator internally.

You can useasync_generator with any async library. It works greatwithasyncio, or Twisted, or whatever you like. (But we think Triois pretty sweet.)

About

Making it easy to write async iterators in Python 3.5

Topics

Resources

License

Unknown and 2 other licenses found

Licenses found

Unknown
LICENSE
Apache-2.0
LICENSE.APACHE2
MIT
LICENSE.MIT

Code of conduct

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

No packages published

Contributors5


[8]ページ先頭

©2009-2025 Movatter.jp