Open this example in the interactive library
Source code
import asyncio
async def main():
lock, values = asyncio.Lock(), []
async def append(n):
async with lock:
values.append(n)
await asyncio.sleep(0)
values.append(-n)
await asyncio.gather(append(1), append(2))
print(values)
asyncio.run(main())Sample output
[1, -1, 2, -2]
