I tried asynchronous processing using asyncio

Trigger

I didn't have a good sample code for ʻasyncio`, but On-demand data in Python, Part 3 Coroutines and asyncio -on-demand-data-python-3 / index.html) explained with a more specific story that the waiter in the restaurant handles multiple orders, so it is the easiest to understand and helpful. .. Frequently seen Understanding async / await in python3 and [Asynchronous processing in Python: asyncio reverse lookup](https://qiita.com/icoxfog417/ Better than items / 07cbf5110ca82629aca0).

development of

import asyncio
import time

async def start_time(src):
	await asyncio.sleep(src)
	print("START!!!")

async def main_process(span):
	idx = 1
	while True:
		await asyncio.sleep(span)
		num_active_tasks = len([ task for task in asyncio.Task.all_tasks(loop) if not task.done()])
		if num_active_tasks == 1:
			break
		print("[run:{}]{}Seconds have passed".format(num_active_tasks, idx * span))
		idx += 1


async def end_time(src):
	await asyncio.sleep(src)
	print("END!!!")

if __name__ == "__main__":
	loop = asyncio.get_event_loop()
	try:
		loop.run_until_complete(
			asyncio.gather(
				start_time(10),
				main_process(1),
				end_time(20)
			)
		)
	finally:
		loop.close()

The point of interest is len([ task for task in asyncio.Task.all_tasks(loop) if not task.done()]) You can get the currently resident task by doing ʻasyncio.Task.all_tasks (loop) if not task.done () `.

result

The output result is as follows. If only the main process is running, it is out of the loop.

[run:3]1 second has passed
[run:3]2 seconds have passed
[run:3]3 seconds have passed
[run:3]4 seconds have passed
[run:3]5 seconds have passed
[run:3]6 seconds have passed
[run:3]7 seconds have passed
[run:3]8 seconds have passed
[run:3]9 seconds have passed
START!!!
[run:2]10 seconds have passed
[run:2]11 seconds have passed
[run:2]12 seconds have passed
[run:2]13 seconds have passed
[run:2]14 seconds have passed
[run:2]15 seconds have passed
[run:2]16 seconds have passed
[run:2]17 seconds have passed
[run:2]18 seconds have passed
[run:2]19 seconds have passed
END!!!

in conclusion

ʻAsyncio.Task.all_tasks () is deprecated in Python 3.7 and later and will be removed in 3.9. Especially in the part of ʻasyncio, the writing style seems to change considerably every time the version goes up. (See Python3.8 doc Task Object)

Referenced link

-Python3.6 doc 18.5.3. Tasks and coroutines -On-demand data in Python, Part 3 Coroutines and asyncio -Understanding async / await for python3 -Asynchronous processing in Python: asyncio reverse lookup reference

Recommended Posts

I tried asynchronous processing using asyncio
I tried using parameterized
I tried using mimesis
I tried using anytree
I tried using aiomysql
I tried using Summpy
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using ngrok
I tried using face_recognition
I tried using Jupyter
I tried using Heapq
I tried using doctest
I tried using folium
I tried using jinja2
I tried using folium
I tried using time-window
I tried using easydict (memo).
I tried face recognition using Face ++
I tried using Random Forest
I tried using BigQuery ML
I tried using Amazon Glacier
I tried using git inspector
[Python] I tried using OpenPose
Asynchronous http communication using asyncio
I tried 100 language processing knock 2020
I tried using magenta / TensorFlow
I tried using Slack emojinator
I tried using Rotrics Dex Arm # 2
I tried 100 language processing knock 2020: Chapter 3
I tried using Rotrics Dex Arm
I tried using GrabCut of OpenCV
I tried using Thonny (Python / IDE)
I tried 100 language processing knock 2020: Chapter 1
I tried reinforcement learning using PyBrain
I tried deep learning using Theano
Somehow I tried using jupyter notebook
[Kaggle] I tried undersampling using imbalanced-learn
I tried using the checkio API
[Python] I tried using YOLO v3
I tried 100 language processing knock 2020: Chapter 2
I tried using Amazon SQS with django-celery
I tried using Azure Speech to Text.
I tried the asynchronous server of Django 3.0
I tried using Twitter api and Line api
Asynchronous processing using Linebot in Job queue
I tried playing a ○ ✕ game using TensorFlow
I tried using YOUTUBE Data API V3
I tried using Selenium with Headless chrome
I tried drawing a line using turtle
[Kaggle] I tried ensemble learning using LightGBM
I tried using PyEZ and JSNAPy. Part 2: I tried using PyEZ
I tried using Bayesian Optimization in Python