Hacker News new | past | comments | ask | show | jobs | submit login
How async/await works in Python 3.5 (snarky.ca)
63 points by brettcannon on Feb 13, 2016 | hide | past | favorite | 10 comments



So it seems it would be able to have goless in python3 with this? The combination of spawning a function and using channels between them is great. Specially the optional numeric argument of chan() where you can specify whether it's blocking or not, having a send buffer etc. Using an event loop with async/await could work nicely. Or also a version using real threads instead.

https://goless.readthedocs.org/en/latest/


It looks different than in Go, but works the same (when simulating a handshake for synchronous channels).

#!/usr/bin/env python3.5

import asyncio

async def give(start, end, c, k): for i in range(start, end): await c.put(i) await k.get() # sync send await c.put(None) # simulate closing of channel

async def consume(c, k): while True: x = await c.get() await k.put(True) # sync recv if x is None: # channel closed return print(x)

loop = asyncio.get_event_loop() c, k = asyncio.Queue(), asyncio.Queue() tasks = [asyncio.ensure_future(give(3, 7, c, k)), asyncio.ensure_future(consume(c, k))] try: loop.run_until_complete(asyncio.wait(tasks)) finally: loop.close()


Ah, found a https://github.com/stuglaser/pychan which could be used with threads.


Nice-- I've found async/await really useful in C#/.NET and didn't realize it was making its way into Python (I'm still mostly writing Python 2.7 code here for availability reasons).

A little disappointed that it doesn't look like there's really support for it in the standard library's networking packages, though... hopefully more will be coming in 3.6 or 3.7?


The way the standard library is managed in terms of new things is it needs to be proved in the community for a year or more before we consider adding it. Since async is so new it's a bit early to get be changing things until we see how the community chooses to approach this new style of programming.


The fun thing is that you could do the same thing in C# before async/await :-). http://tomasp.net/blog/csharp-async.aspx/


C# a actually inspired Python's implementation. Python historically takes ideas from other languages and refines them to Python's style.


Lots of pretty good aio libraries are being developed openly on Github in this organization:

https://github.com/aio-libs

This includes lots of network libraries and database clients.


This is such a good blog post. Thank you for writing it.


Welcome! Glad you liked it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: