Created
August 7, 2019 22:53
-
-
Save bukowa/2ff7e10a62da115da0497f0b70d9353a to your computer and use it in GitHub Desktop.
async loop
This file contains 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 | |
import time | |
sema = asyncio.Semaphore(value=5) | |
async def worker(): | |
async with sema: | |
print("Start") | |
await asyncio.sleep(3) | |
print("Finish") | |
async def add(): | |
while True: | |
await asyncio.sleep(0) | |
async with sema: | |
asyncio.ensure_future(worker()) | |
async def main(): | |
asyncio.ensure_future(add()) | |
loop = asyncio.get_event_loop() | |
asyncio.ensure_future(main()) | |
loop.run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment