Writing

Protecting Shared State Across Coroutines with asyncio.Lock

A common misconception about Python's asyncio is that because it runs on a single thread via an event loop, race conditions cannot occur.

While it is true that two coroutines cannot execute CPU instructions simultaneously on the same thread, context switches occur whenever a coroutine yields control at an await expression. If multiple coroutines read and write shared global or class state across those suspension points, execution interleaves unpredictably.

Just as with multithreaded programming (threading.Lock), Python provides asyncio.Lock to guarantee mutually exclusive access to shared resources.

[!NOTE] Python Version Note In older Python code (pre-3.10), instantiating asyncio.Lock() outside an active event loop or passing an explicit loop argument was common. Starting with Python 3.10+, the loop parameter is deprecated/removed; always instantiate asyncio.Lock() inside an async function or pass it through your coroutines, and run your program using asyncio.run().

The Race Condition Problem (Without Lock)

Consider two coroutines that update a global variable owner, pause briefly to simulate an asynchronous I/O operation (such as a database query or network request), and then read owner again:

import asyncio
import random

owner = None


async def coro1():
    global owner

    owner = "IAMMAN"
    print(f"I am owner, {owner}")
    await asyncio.sleep(random.random())
    print(f"{owner} is man.")


async def coro2():
    global owner

    owner = "IAMWOMAN"
    print(f"I am owner, {owner}")
    await asyncio.sleep(random.random())
    print(f"{owner} is woman.")


async def main():
    for _ in range(5):
        await asyncio.gather(coro1(), coro2())
        await asyncio.sleep(0.1)


if __name__ == "__main__":
    asyncio.run(main())

Output:

I am owner, IAMMAN
I am owner, IAMWOMAN
IAMWOMAN is woman.
IAMWOMAN is man.
I am owner, IAMMAN
I am owner, IAMWOMAN
IAMWOMAN is woman.
IAMWOMAN is man.
...

Notice what happened: coro1 set owner = "IAMMAN", but while it was paused during await asyncio.sleep(), coro2 ran and overwrote owner = "IAMWOMAN". When coro1 resumed, it read the modified value and printed "IAMWOMAN is man.".

Synchronizing with asyncio.Lock

By protecting the critical section with an asyncio.Lock, only one coroutine can hold the lock at any given moment. Any other coroutine trying to acquire it will pause until the current holder releases it.

The recommended and safest approach is using the asynchronous context manager (async with lock:):

import asyncio
import random

owner = None


async def coro1(lock: asyncio.Lock):
    global owner

    # Explicit acquire/release pattern (or prefer 'async with lock:'):
    await lock.acquire()
    try:
        owner = "IAMMAN"
        print(f"I am owner, {owner}")
        await asyncio.sleep(random.random())
        print(f"{owner} is man.")
    finally:
        lock.release()


async def coro2(lock: asyncio.Lock):
    global owner

    # Idiomatic context manager pattern:
    async with lock:
        owner = "IAMWOMAN"
        print(f"I am owner, {owner}")
        await asyncio.sleep(random.random())
        print(f"{owner} is woman.")


async def main():
    lock = asyncio.Lock()

    for _ in range(5):
        await asyncio.gather(coro1(lock), coro2(lock))
        await asyncio.sleep(0.1)


if __name__ == "__main__":
    asyncio.run(main())

Output:

I am owner, IAMMAN
IAMMAN is man.
I am owner, IAMWOMAN
IAMWOMAN is woman.
I am owner, IAMMAN
IAMMAN is man.
I am owner, IAMWOMAN
IAMWOMAN is woman.
...

Now each coroutine's critical section executes sequentially without interference.

Practical Takeaways

  • Locks reduce concurrency: Locking entire tasks defeats the performance benefits of asynchronous programming. Keep the locked section as short and targeted as possible—only protecting the specific state transition that must remain atomic.
  • Prefer async with lock:: Using context managers ensures that even if an exception occurs inside the critical section, the lock is reliably released without deadlocking your application.