[Python] How to use threading.Lock in async function while object can be accessed from...

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I want to use threading.Lock() in a async function, asyncio.Lock() is not thread safe so I cannot do with await asyncio.Lock():. The reason I need to use threading.Lock() is because this object may be accessed by multiple treads, hence it is used in a web app and the server running it can spin up many threads. What is an effective way of doing so ? So far I've tried a simple function that uses the lock:

    1)

    async def main():
    with await threading.Lock():
    a = 6
    return a

    TypeError: object _thread.lock can't be used in 'await' expression


    async def main():
    async with threading.Lock():
    a = 1564666546
    return a

    AttributeError: __aexit__

    Continue reading...

Compartilhe esta Página