1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Is it safe that when Two asyncio tasks access the same awaitable object?

Discussão em 'Python' iniciado por Stack, Setembro 17, 2025.

  1. Stack

    Stack Membro Participativo

    Simply speaking, thread-safe means that it is safe when more than one thread access the same resource and I know Asyncio use a single thread fundamentally.

    However, more than one Asyncio Task could access a resource multiple time at a time like multi-threading.

    For example DB connection(if the object is not thread-safe and supports Asyncio operation).

    1. Schedule Task A and Task B accessing the same DB object.
    2. IO Loop executes Task A.
    3. Task A await IO operation on the DB object.(it will take long time enough)
    4. IO Loop executes Task B
    5. Step3's IO operation is still in progress(not done).
    6. Task B await IO operation on the same DB object.
    7. Now Task B is trying to access the same object at a time.

    Is it completely safe in Asyncio and if so, what does it make safe?

    Continue reading...

Compartilhe esta Página