SHARE
TWEET

Untitled

a guest Aug 14th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import asyncio
  2.  
  3. def main1():
  4.     print(1)
  5.  
  6. def main2():
  7.     print(2)
  8.  
  9. def main3():
  10.     print(3)
  11.  
  12. def main4():
  13.     print(4)
  14.  
  15. async def do_main1(semaphore):
  16.     async with semaphore:
  17.         print('Start work #1')
  18.         main1()
  19.         # optionally do a lot of work that will consume memory
  20.         await asyncio.sleep(1)
  21.         print('End work #1')
  22.  
  23. async def do_main2(semaphore):
  24.     async with semaphore:
  25.         print('Start work #2')
  26.         main2()
  27.         # optionally do a lot of work that will consume memory
  28.         await asyncio.sleep(1)
  29.         print('End work #2')
  30.  
  31. async def do_main3(semaphore):
  32.     async with semaphore:
  33.         print('Start work #3')
  34.         main3()
  35.         # optionally do a lot of work that will consume memory
  36.         await asyncio.sleep(1)
  37.         print('End work #3')
  38.  
  39. async def do_main4(semaphore):
  40.     async with semaphore:
  41.         print('Start work #4')
  42.         main4()
  43.         # optionally do a lot of work that will consume memory
  44.         await asyncio.sleep(1)
  45.         print('End work #4')
  46.  
  47. async def main():
  48.     tasks = []
  49.  
  50.     semaphore = asyncio.BoundedSemaphore(2)
  51.  
  52.     tasks.append(asyncio.ensure_future(do_main1(semaphore)))
  53.     tasks.append(asyncio.ensure_future(do_main2(semaphore)))
  54.     tasks.append(asyncio.ensure_future(do_main3(semaphore)))
  55.     tasks.append(asyncio.ensure_future(do_main4(semaphore)))
  56.     await asyncio.gather(*tasks)
  57.  
  58. if __name__ == "__main__":
  59.  
  60.     loop = asyncio.get_event_loop()
  61.     #loop.set_debug(True)
  62.     result = loop.run_until_complete(main())
  63.  
  64.     loop.close()
  65.  
  66.     if result:
  67.             print(f"Задание {result}")
  68.             sleep(2)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top