How asyncio works? and event loop vs exceptions

Marco S. mail.python.org at marco.sulla.e4ward.com
Fri Jul 22 20:27:44 EDT 2016


I'm developing a web app based on aiohttp, and I find the event loop
concept very interesting. I never programmed with it before, but I
know that node.js and GUIs are based on it.

What I can't understand is how asyncio make it possible to run
multiple tasks concurrently, since it's single threaded (if you don't
use loop.run_in_executor()). I just tried to imagine that it should
act as a cpu scheduler. Is this true? If so, how and when asyncio
decide to switch to another task?

Furthermore I have a question about exceptions in asyncio. If I
understand well how it works, tasks exceptions can be caught only if
you wait for task completion, with yield from, await or
loop.run_until_complete(future). But in this case the coroutine blocks
the execution of the program until it returns. On the contrary you can
execute the coroutine inside an asyncio task and it will be
non-blocking, but in this case exceptions can't be caught in a try

statement.

Is this correct? If so, asyncio programming style can't be a little
divergent from what was the philosophy of Python until now?



More information about the Python-list mailing list