Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- def main1():
- print(1)
- def main2():
- print(2)
- def main3():
- print(3)
- def main4():
- print(4)
- async def do_main1(semaphore):
- async with semaphore:
- print('Start work #1')
- main1()
- # optionally do a lot of work that will consume memory
- await asyncio.sleep(1)
- print('End work #1')
- async def do_main2(semaphore):
- async with semaphore:
- print('Start work #2')
- main2()
- # optionally do a lot of work that will consume memory
- await asyncio.sleep(1)
- print('End work #2')
- async def do_main3(semaphore):
- async with semaphore:
- print('Start work #3')
- main3()
- # optionally do a lot of work that will consume memory
- await asyncio.sleep(1)
- print('End work #3')
- async def do_main4(semaphore):
- async with semaphore:
- print('Start work #4')
- main4()
- # optionally do a lot of work that will consume memory
- await asyncio.sleep(1)
- print('End work #4')
- async def main():
- tasks = []
- semaphore = asyncio.BoundedSemaphore(2)
- tasks.append(asyncio.ensure_future(do_main1(semaphore)))
- tasks.append(asyncio.ensure_future(do_main2(semaphore)))
- tasks.append(asyncio.ensure_future(do_main3(semaphore)))
- tasks.append(asyncio.ensure_future(do_main4(semaphore)))
- await asyncio.gather(*tasks)
- if __name__ == "__main__":
- loop = asyncio.get_event_loop()
- #loop.set_debug(True)
- result = loop.run_until_complete(main())
- loop.close()
- if result:
- print(f"Задание {result}")
- sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement