Coding Global Background
Coding Global

Async Partial Function Decorator

Archiviert 3 years ago
1 Nachrichten
1 Mitglieder
Erstellt 3 years ago
Aktualisiert 3 years ago
In Discord öffnen
D
Dot
Verified
this is my decorator
def pool_to_cursor(func:Callable[..., Any], pool:aiomysql.pool.Pool):
    @functools.wraps(func)
    async def wrapper(*args: Any, **kwargs: Any):
        conn = await pool.acquire()
        cursor = await conn.cursor()
        result = await func(cursor,*args,**kwargs)
        await conn.commit()
        await cursor.close()
        conn.close()
        return result
    return wrapper

and I want to call it without having to add the (pool) everytime I do @pool_to_cursor
so I added this
async def main() -> None:
    global run_pool_to_cursor
    pool = await generate_mariadb_connection()
    run_pool_to_cursor = functools.partial(pool_to_cursor,pool=pool)

but it gives me the error NameError: name 'run_pool_to_cursor' is not defined.

This is how I'm running the code btw
if __name__=="__main__":
    asyncio.run(main())

Antworten (1)