Last active
February 12, 2023 10:47
-
-
Save apeyroux/557316be5ed8142faa590cfb786ffb6a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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