Python 3.6 异步 aioodbc 阻塞

Python 3.6 async aioodbc blocking

我希望使用带有异步信号量的 aioodbc 将行插入数据库。下面将一些行写入目标数据库,但似乎锁定在 Sempahore value +1 附近。关于如何返工或解决 block/contention?

的任何建议

Table定义:

create table async_testing (
    insert_id int null
)

异步代码:

import asyncio
import aioodbc

loop = asyncio.get_event_loop()

async def odbc_insert_worker(semaphore, value, conn):
    await semaphore.acquire()
    print("Acquire Semaphore")
    async with conn.cursor() as cur:
        await cur.execute('INSERT INTO async_testing VALUES (?)', value)
    print("Release Semaphore")
    semaphore.release()

async def db_main(loop, values):
    dsn="foo"

    values = list(values)
    db_semaphore = asyncio.Semaphore(value=15)

    async with aioodbc.create_pool(dsn=dsn, loop=loop, autocommit=True) as pool:
        async with pool.acquire() as conn:
            tasks = [odbc_insert_worker(db_semaphore, value, conn) for value in values]
            await asyncio.gather(*tasks)

fmt_vals = range(0,1000)

loop.run_until_complete(db_main(loop, fmt_vals))

感谢 aiolibs 频道中 @jettify 的帮助,此解决方案有效:

import asyncio
import aioodbc
from concurrent.futures import ThreadPoolExecutor

loop = asyncio.get_event_loop()

async def odbc_insert_worker(conn, value):
    async with conn.cursor() as cur:
        await cur.execute('insert into async_testing values (?)', value)

async def db_main(loop, values):
    dsn="foo"

    values = list(values)

    async with aioodbc.create_pool(dsn=dsn, loop=loop, executor=ThreadPoolExecutor(max_workers=3), autocommit=True) as pool:
        tasks = [do_insert(pool, value) for value in values]
        await asyncio.gather(*tasks)

async def do_insert(pool, value):
    async with pool.acquire() as conn:
        await odbc_insert_worker(conn, value)

fmt_vals = range(0,1000)

loop.run_until_complete(db_main(loop, fmt_vals))