Benefits of asyncio

Terry Reedy tjreedy at udel.edu
Mon Jun 2 16:07:01 EDT 2014


On 6/2/2014 1:40 PM, Aseem Bansal wrote:

The following supplement Ian's answer.

> I read in these groups that asyncio is a great addition to Python 3.
> I have looked around and saw the related PEP which is quite big BTW
> but couldn't find a simple explanation for why this is such a great
> addition. Any simple example where it can be used?

asyncio replaces the very old asyncore, which has problems, is beyond 
fixing due to its design, and is now deprecated. So look up used for 
asyncore. You could think of asyncio as a lightweight version or core of 
other async packages, such as Twisted or Tornado. What are they good 
for. I admit that you should now have to answer the question so 
indirectly. One generic answer: carry on 'simultaneous' conversions with 
multiple external systems.

asyncio lets you write platform independent code while it makes good use 
of the asynchronous i/o available on each particular system. Async-i/o 
is one area where Windows has made advances over posix. But the models 
are different, and if one uses Windows' i/o completion as if it were 
posix poll/select, it works poorly. Running well on both types of 
systems was a major challenge.

> It can be used to have a queue of tasks?

Try set of tasks, as the sequencing may depend on external response times.

> Like threads? Maybe light weight threads?

Try light-weight thread, manages by Python instead of the OS.
I believe greenlets are a somewhat similar example.

> Those were my thoughts but the library reference
> clearly stated that this is single-threaded.

Meaning, asyncio itself only uses one os thread. The application, or 
individual tasks, can still spin off other os threads, perhaps for a 
long computation.

 > So there should be some waiting time in between the tasks.

I do not understand this. asyncio should switch between tasks faster 
than the OS switches between threads, thus reducing waiting time.

-- 
Terry Jan Reedy




More information about the Python-list mailing list