DEV Community

Async programming in Python with asyncio

Andrea Benfatti on July 15, 2018

For people coming from JavaScript asynchronous programming is nothing new, but for python developers getting used to async functions and future (th...
Collapse
 
mi2pankaj profile image
Pankaj Kumar Katiyar

This is so far the best and most informative article on async concurrency in python. Love it. Thanks a lot for posting online.

Collapse
 
daolf profile image
Pierre • Edited

Hello and thanks for this great article.

Just one remark about multithreading though.
Each of Python threads will not execute line 2 at the same time. But something like this can happen:
thread 1 execute line 2 -> thread 2 execute line 2 -> thread 1 execute line 3 -> thread 2 execute line 3 !!! (size overflow)
Race condition.

Because of the GIL (Global Interpreter Lock) no line of Python can be executed at the same time when doing multithreading.

Collapse
 
welldone2094 profile image
Andrea Benfatti

That's totally true. I didn't talk about it because it wasn't the main topic. Anyway on this subject you can use Processes instead of thread which don't suffer from GIL but i think they bring some overhead with them

Collapse
 
mtoto_lekgwathi profile image
Mapogo Lekgwathi

I think this is somewhat unhelpful, what if I write mainly synchronous code and want some tasks to execute asynchronously? (I don't care about what the async function returns)

E.g I want to run a http request in the background every time something happens, but I don't want to create a thread, and run it

Collapse
 
welldone2094 profile image
Andrea Benfatti

unfortunately, I think that what you are asking is not possible, if the program runs in a single process/thread it needs to know when to switch between tasks. I think for your problem the best solution is to use multiple threads which make the code really simple and you don't need to decide when to switch context with just minimal overhead.

Collapse
 
eljayadobe profile image
Eljay-Adobe

On a related note: pythonclock.org/

Collapse
 
welldone2094 profile image
Andrea Benfatti

cannot wait for it! I have a strong unmotivated hate for python2

Thread Thread
 
eljayadobe profile image
Eljay-Adobe

I started using Python 2 around 2001, and had a very long laundry list of Things I Hate About Python.

When Python 3 came out, I had all but given up on Python. Took a look at Python 3, and out of my list of hate, everything-except-one-thing was fixed in Python 3.

The only thing that wasn't "fixed" was offside rule. I know offside rule is part of the heart and soul of Python. It will never change. Even after all this time, it still feels like fingernails on a chalkboard to me.

I can accept that one point of agree-to-disagree. (Heck, my Things I Hate About C++ list is about x1000 times longer than my Python 2 hate list. Python 3 with but a single hate item is bliss in comparison to C++!)

Collapse
 
lauriy profile image
Lauri Elias

asycnio <- typo

Collapse
 
welldone2094 profile image
Andrea Benfatti

fixed, thanks

Collapse
 
abdurrahmaanj profile image
Abdur-Rahmaan Janhangeer

I suggest you watch the following video

which one?

Collapse
 
welldone2094 profile image
Andrea Benfatti

sorry, i may have forgot to copy it from my website. This is the video, i will update the post as well
youtube.com/watch?v=M-UcUs7IMIM

Collapse
 
welldone2094 profile image
Andrea Benfatti

I cannot see the link anymore, anyway I didn't talk about ensure_future to avoid creating confusion and so far I have never needed it.