[Python] How do I create thread-safe asyncio background tasks?

Discussão em 'Python' iniciado por Stack, Maio 29, 2025.

  1. Stack

    Stack Membro Participativo

    I have an event loop running in a separate thread from the main thread, and I would like to add a background task to this event loop from the main thread using the asyncio.create_task function.

    I am able to achieve this using the asyncio.run_coroutine_threadsafe function, but that ends up returning a concurrent.futures._base.Future object as opposed to the asyncio.Task object that the asyncio.create_task function returns.

    I would really like to have a Task object returned as I'm making use of the "name" attribute of the Task object in my application logic. Is there any way to implement this?

    Continue reading...

Compartilhe esta Página