19

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:

  • So far I've only used asyncio.coroutine in combination with yield from, but this comparison suggests I should reconsider.
  • A server might either be created using loop.create_server(MyProtocol) or asyncio.start_server(my_connection_handler) - when would I use either?
  • What is proper hygiene in managing loops' life cycle (e.g. loop.close() after loop.run_forever())?
  • I have yet to understand why Tasks are required in addition to Futures.
  • What if I want a class constructor to be non-blocking (i.e. use yield from, which appears to be invalid)?
  • Can class getters be asynchronous (i.e. combining @property and asyncio.coroutine)?
  • How do I know whether any given function is asynchronous? For example, I'd expect 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.

Community
  • 1
  • 1
AnC
  • 4,099
  • 8
  • 43
  • 69
  • If you are asking for book or tutorial I cannot help you. But I can answer on maybe any particular question about asyncio. – Andrew Svetlov Jan 03 '15 at 13:46
  • Thanks. Right now, I'm not sure it makes sense for me to turn the above into individual questions, as that wouldn't help my understanding at the conceptual level. Perhaps I'm overthinking it though, so will consider that. – AnC Jan 03 '15 at 14:45

1 Answers1

3

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

Community
  • 1
  • 1
Morgan
  • 300
  • 1
  • 5
  • Thanks, knowing about Executor might well come in handy. However, "you can make any task non-blocking" assumes that I already know which parts are blocking - but how would I? (See `StreamWriter` example above.) I'd also found the articles you mention - hopefully there'll be more coverage in the future. – AnC Jan 06 '15 at 06:02
  • For streamWriter, it is a Streaming Object, which means it answers to any messages it receives, just like a server, so it is not blocking. it is just listening. As to dertermine what is blocking, well in my case,i did a quick test and tried to run two tasks at the same time and realized it was blocking and then i understood. My task was taking a long time, so if i as trying to run two in the same thread, it blocked the other one, hence the executors so the tasks have their own thread. That's how i understood it. But there are a lot of uses cases, and you are right, they are not documented. – Morgan Jan 06 '15 at 14:39
  • Even if Twisted Matrix is different, it helped to read about how it works: http://stackoverflow.com/questions/6117587/twisted-making-code-non-blocking. I also found out this: https://pypi.python.org/pypi/aioprocessing/0.0.1 , which is trying to solve the problem through libraries. – Morgan Jan 06 '15 at 14:40
  • 1
    I found [this](http://stackabuse.com/python-async-await-tutorial/) helpful - which is more recent and up to date specifying the changes in 3.5 and on. – Mark Aug 05 '16 at 10:58