Hacker News new | ask | show | jobs
by thuffy 4083 days ago
Python 3's asyncio / co-routines is the reason to write any new project in Python 3.

The API is tiny, and the result is beautiful.

If you haven't seriously tried using it to implement something that really benefits from such computer science innovation, then you have simply seen nothing of Python 3 yet. There are many types of logic that are vastly simplified with such a programming paradigm. Faster to code, and far simpler to debug. By orders of magnitude. One example you could try if you cannot envision one is to implement a high performance network protocol. I for instance implemented the SSH protocol from scratch (RFCs) in a matter of weeks. I though it would take months before I tried, especially since I had never programmed in Python before! As I said, the API is tiny. Learning it (Python and Asyncio at once) was so easy and ended up taking such an insignificant amount of time compared to the vast quantities of time and headache saved, that, well, I just had to write this post with all that extra time and energy!

Check it out! You will find that it is beyond worth just addressing whatever fears or problems you have with making the switch.

6 comments

Im sure most people will start to use Python 3 for one of those large new features, but to me its all the small QoL changes for the standard library which I constantly see when searching for tricky problems and finding them in SO.
asyncio seems to me like it should be the biggest killer app for Python 3 - a universal replacement for the sprawling, confusing, and opposing async options out there for Python 2.x (Twisted, Tornado, gevent...).

I'm hoping that aiohttp, or a similar library, further matures and we start seeing apps built on it. Developing asynchronous HTTP code in Python 2.x is significantly harder than throwing together a Node app, where things are "asynchronous by default" (unlike, say, Flask or Django). Having a universal, built-in event loop removes a huge point of confusion and learning for async development in Python, and can hopefully bring interest from developers who would normally look to Node.

As a big fan (and user) of Twisted, I'd like to see a discussion of why I might want to use asyncio instead, besides the So-You-Can-Use-Py3 reason.
I 'll add my experience with asyncio and twisted:

1) debugging is way better in asyncio, with ipdb you can step in co-routine. In twisted, you will end up somewhere in internals.

2) asyncio is end of unhanded error in deferred :) (or you need to remember to add addErrback everywhere, even after addColbecks and addBoth)

3) with trial testing is frustrating, sometimes error raised in wrong test because of global event loop (for instance you forgot to mock external call). In asyncio world loop passed explicitly and this is good for testing.

Well here's a great blog post on how to use the @asyncio.coroutine decorator to do asynchronous/non-blocking I/O in this style, as opposed to JS-style callbacks. I think the control flow is a lot clearer with coroutines as opposed to callbacks. I've never used Twisted though so I'm not sure how asyncio compares to that library.

http://sahandsaba.com/understanding-asyncio-node-js-python-3...

Is there a thorough getting started guide for asyncio floating someplace on the web? I tried to pick up asyncio a few weeks ago. The official documentation assumes a lot of background that I don't possess, despite being a long-time Python user. Each method and class seems to be documented, but conceptual and end-to-end examples were much less common. Thanks in advance!
asyncio is just nice API for async programing and event loop management. You should start from something high level in order to obtain asyncio ideas, like scraping [1] or try web development with aiohttp [2]. And then if you want to write some library or port database driver, look for examples at aio-libs[3] github organization.

[1] http://compiletoi.net/fast-scraping-in-python-with-asyncio.h...

[2] https://github.com/KeepSafe/aiohttp

[3] https://github.com/aio-libs

Any interest in opening that SSH implementation up to the world. I wouldn't mind getting my hands on it and (maybe) even contributing.
Good news then!

I'll be releasing the greater project shortly as GPL, with the SSH implementation as LGPL! I'll announce on HN for sure then.

Guido, if you want to put it in Python itself, you can have it in whatever license you need for that.

I don't think so especially since it still uses bytes to output to a webserver, even go is way better for this kind of tiny library. For bigger projects i would still go the java route especially since there is karyon from netflix.