Skip to content

Instantly share code, notes, and snippets.

@apeyroux
Last active February 12, 2023 10:47
Show Gist options
  • Save apeyroux/557316be5ed8142faa590cfb786ffb6a to your computer and use it in GitHub Desktop.
Save apeyroux/557316be5ed8142faa590cfb786ffb6a to your computer and use it in GitHub Desktop.
import asyncio
shared_list = []
list_lock = asyncio.Lock()
async def add_to_list(item):
async with list_lock:
shared_list.append(item)
async def remove_from_list(item):
async with list_lock:
shared_list.remove(item)
async def main():
tasks = []
print("Je remplis la liste partagée")
for i in range(10):
tasks.append(asyncio.create_task(add_to_list(i)))
await asyncio.gather(*tasks)
print(shared_list)
print("Je vide la liste partagée")
for i in range(10):
tasks.append(asyncio.create_task(remove_from_list(i)))
await asyncio.gather(*tasks)
print(shared_list)
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment