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?
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.
There is lots of info that is missing in your question.
threading.Lock
.await
) between writes (to the list) in the request handler? If yes, then you have to wrap your list in a asyncio.Lock
.multiprocessing.Lock
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With