TLDR: I'm looking for a comprehensive or authoritative explanation (tutorial/book/presentation/...) of asyncio for application developers.
While I have a decent understanding of event loops and futures/deferreds/promises (largely thanks to JavaScript), somehow the intricacies of Python's asyncio continue to confound me. asyncio seems significantly more complex than what I'm used to - presumably because it was partly designed for low-level compatibility with existing implementations (Twisted, Tornado etc.) and because it allows for multiple event loops in separate threads.
As far as I can tell, there's no comprehensive walkthrough of the basic concepts, so I've consulted the official docs as well as various articles and presentations across the web.
Yet I remain unsure of my understanding, quite possibly because it's not always clear what's relevant at the application level or if you don't need to worry about the aforementioned alternatives. (Many resources seem to assume familiarity with Twisted et al.)
A few examples of things that induced uncertainty for me:
asyncio.coroutine
in combination with yield from
, but this comparison suggests I should reconsider.loop.create_server(MyProtocol)
or asyncio.start_server(my_connection_handler)
- when would I use either?loop.close()
after loop.run_forever()
)?Task
s are required in addition to Future
s.yield from
, which appears to be invalid)?@property
and asyncio.coroutine
)?StreamWriter.write
to be non-blocking, but I don't know whether that's actually the case.I'm not asking for answers to these particular questions, they merely illustrate how I'm struggling at a conceptual level.
The asyncio module was added to Python in version 3.4 as a provisional package.
The asyncio package has been included in the standard library on a provisional basis.
asyncio requires Python 3.3 or later! The asyncio module is part of the Python standard library since Python 3.4. asyncio is a free software distributed under the Apache license version 2.0.
I am like you, looking for answers, but i can help you withone thing:
Regarding the non-blocking problem:
I created a program that uses asynchronous loops to listen to twitter feeds,
i found the answer here: asyncio yield from concurrent.futures.Future of an Executor
Basically, using the executor, you can make any task non-blocking. Just a warning, my tasks are independant and do not need syncing, i just needed them to become non blocking. If you need them to wait for each toher, youhave to use semaphore
Here os how i did it :
@asyncio.coroutine
def boucle_deux():
#faire attendre la boucle si pas bcp de mots
while True:
print("debut du deux")
value = t.next()
future2 = loop.run_in_executor(None, mention, "LQNyL2xvt9OQMvje7jryaHkN8",
"IRJX6S17K44t8oiVGCjrj6XCVKqGSX9ClfpGpfC467rajqePGb",
"2693346740-km3Ufby8r9BbYpyzcqwiHhss22h4YkmnPN4LnLM",
"53R8GAAncFJ1aHA1yJe1OICfjqUbqwcMR38wSqvbzsQMB", 23, value)
response2 = yield from future2
yield from asyncio.sleep(5)
print("fin du deux")
asyncio.Task(boucle_deux())
Here are a few links i found that helped me better understand:
http://www.drdobbs.com/open-source/the-new-asyncio-in-python-34-servers-pro/240168408
http://sahandsaba.com/understanding-asyncio-node-js-python-3-4.html
http://www.drdobbs.com/open-source/the-new-asyncio-module-in-python-34-even/240168401
http://ntoll.org/article/asyncio
Sure it is not a book, but it is a good starting place
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With