Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.39 KB | None | 0 0
  1. import asyncio
  2.  
  3. g_lock = asyncio.Lock()
  4.  
  5. def get_func(i):
  6.     async def func():
  7.         while True:
  8.           async with g_lock:
  9.             print(i)
  10.             await asyncio.sleep(i/10)
  11.     return func
  12.  
  13.  
  14. funcs = [get_func(i) for i in range(3)]
  15.  
  16.  
  17. async def main():
  18.   tasks = [asyncio.create_task(func()) for func in funcs]
  19.   for task in tasks:
  20.     await task
  21.  
  22. asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement