0

Background

I have inherited code that uses multiple event loops to handle read/writing to/from multiple objects created using websockets.serve. These coroutines need to access a common list.

Scenario

  • coroutine A in event loop X
  • coroutine B in event loop Y

Questions

  1. Do I need to protect the access to the common list?
  2. If I do need to protect access to the list, will asyncio.Lock() be sufficient?
1
  • Hi, I'm curious whether my answer resolved your question? Commented Aug 19, 2020 at 9:42

1 Answer 1

2

I assume the event loops run in parallel, each in its own thread. In that case you would treat shared data exactly as you would data shared between different threads. Specifically:

  1. It will depend on what you are doing with the lists. If one thread is reading and the other is calling something like append, you don't need synchronization because you're protected by the GIL. But if you are doing something more complex, e.g. deleting the items directed by the reader thread, you will need to lock to ensure you don't lose items due to concurrent modification.

  2. asyncio.Lock is definitely not the correct locking device to use because it is designed for use within one event loop, i.e. it's single-threaded. You can't use a threading.Lock either because it's non-async and would block the whole event loop. You could probably use loop.call_soon_threadsafe to synchronize between event loops, but it's not trivial.

The code you inherited uses asyncio in a way it was not intended to be used and you're likely to run into issues. Since the asyncio event loop is designed to scale to an arbitrary number of tasks, I recommend refactoring the code to use a single event loop. Then you'll need no special synchronization, as any code between awaits will naturally form a critical section, and if you need locking across await, you can use asyncio.Lock, which was created for that use case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.