Created
November 25, 2020 03:03
-
-
Save rmporsch/b8189c06970a28279f420a57849f37e4 to your computer and use it in GitHub Desktop.
[Async worker] #python
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 | |
async def queue(task_name): | |
i = 0 | |
while True: | |
if i >= 5: | |
return True | |
print(f"checking queue for task {task_name}") | |
i += 1 | |
await asyncio.sleep(0.1) | |
async def mlstask(task_name): | |
while True: | |
if await queue(task_name): | |
await asyncio.sleep(0.5) | |
print(f"finished process {task_name}") | |
async def create_tasks(num_tasks): | |
loop = asyncio.get_running_loop() | |
tasks = [] | |
for i in range(num_tasks): | |
task = loop.create_task(mlstask(i)) | |
tasks.append(task) | |
def main(): | |
loop = asyncio.get_event_loop() | |
loop.create_task(create_tasks(2)) | |
loop.run_forever() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment