[Tutor] asyncio or threading

Alan Gauld alan.gauld at btinternet.com
Mon Feb 15 14:24:17 EST 2016


On 15/02/16 13:09, CMG Thrissur wrote:

> I just wanted an opinion on the subject, asyncio and threading both seam 
> to do the same job, but i feel threading is better because of the 
> multiple ways i can control it.

They do similar jobs but asyncio uses threading in the background
and I believe it's at OS level not Python native threading (which
has some significant issues).

However, the biggest differences are in the conceptual usage. asyncio is
intended primarily for event driven scenarios where something happens
that triggers a long running (eg. a hundred milliseconds or longer,
say). You add the task to the event loop and it gets processes as and
when it gets scheduled. You don't care and just wait for it to complete
after which it will call your callback function. So if you have a
scenario where tasks can be scheduled and forgotten about then asyncio
is ideal. The obvious use case is a server receiving
messages over a network and returning results when done.

If you want to do something in near real-time with a separate
thread where you comunicate between two (or more) threads then
asyncio is not such a good fit and conventional threading will
likely be better. But it's a lot harder to get right. With asyncio
all the hard threading bits are done for you.

Finally, if you have done any work with NodeJS using Javascript
you are likely to find asyncio a natural fit to your style.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list