quart icon indicating copy to clipboard operation
quart copied to clipboard

Add API to name background tasks

Open agritheory opened this issue 1 year ago • 0 comments

asyncio.create_task() from the standard library allows for identifying tasks with names. This seems particularly handy in case where you want to know about the status of a background task in Quart context and I think it'd be a nice feature to support. In the app that I'm currently working on, I'm using the background task to run an MQTT listener and logger, which can be configured via HTTP/GraphQL.

To ensure compatibility, I'd propose a check for 'name' in the kwargs and passing that to the task creation LOC

An example of how'd I'd like to be able to use this feature:

# called inside @app.before_serving 
async def mqtt_handler():
	broker_url = env.str("MQTT_BROKER_HOST", "localhost")
	broker_port = env.int("MQTT_BROKER_PORT", 1883)
	mqtt_logger = MQTTLogger(app.db, broker_url, broker_port)

	app.add_background_task(mqtt_logger.start, name="MQTTLoggerTask")

agritheory avatar Nov 16 '24 01:11 agritheory