Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 Asyncio shared resources between concurrent tasks

I've got a network application written in Python3.5 which takes advantage of pythons Asyncio which concurrently handles each incoming connection.

On every concurrent connection, I want to store the connected clients data in a list. I'm worried that if two clients connect at the same time (which is a possibility) then both tasks will attempt to write to the list at the same time, which will surely raise an issue. How would I solve this?

like image 877
Cellydy Avatar asked Aug 05 '16 11:08

Cellydy


2 Answers

asyncio does context switching only on yield points (await expressions), thus two parallel tasks are not executed at the same time.

But if race conditions are still possible (it depends on concrete code structure) you may use asyncio synchronization primitives and queues.

like image 88
Andrew Svetlov Avatar answered Oct 26 '22 21:10

Andrew Svetlov


There is lots of info that is missing in your question.

  • Is your app threaded? If yes, then you have to wrap your list in a threading.Lock.
  • Do you switch context (e.g. use await) between writes (to the list) in the request handler? If yes, then you have to wrap your list in a asyncio.Lock.
  • Do you do multiprocessing? If yes then you have to use multiprocessing.Lock
  • Is your app divided onto multiple machines? Then you have to use some external shared database (e.g. Redis).

If answers to all of those questions is no then you don't have to do anything since single-threaded async app cannot update shared resource parallely.

like image 25
freakish Avatar answered Oct 26 '22 22:10

freakish