import asyncio
from weakref import WeakValueDictionary

import aiosqlite


async def dbtask(dbname: str, q: asyncio.Queue):
    async with aiosqlite.connect(dbname) as db:
        while True:  # Change to a delay-based expiry
            try:
                query, f = q.get_nowait()
            except asyncio.QueueEmpty:
                # The connection will only be kept open while
                # there is work to be done.
                return
            result = []
            async with db.execute(query) as cursor:
                async for row in cursor:
                    result.append(row)
            await db.commit()
            f.set_result(result)


dbtasks = WeakValueDictionary()


async def run_query(dbname, query: str) -> str:  # JSON data
    t: asyncio.Task = dbtasks.get(dbname)
    if not t:
        q = asyncio.Queue()
        t = asyncio.create_task(dbtask(dbname, q))
        t.q = q
        dbtasks[dbname] = t

    f = asyncio.Future()
    t.q.put_nowait((query, f))
    result = await f
    return str(result)

aef7b747c94910d2b3cebf4869b8d53f8fd2a6e0
<mask token>

<mask token>


async def dbtask(dbname: str, q: asyncio.Queue):
    async with aiosqlite.connect(dbname) as db:
        while True:
            try:
                query, f = q.get_nowait()
            except asyncio.QueueEmpty:
                return
            result = []
            async with db.execute(query) as cursor:
                async for row in cursor:
                    result.append(row)
            await db.commit()
            f.set_result(result)


<mask token>


async def run_query(dbname, query: str) ->str:
    t: asyncio.Task = dbtasks.get(dbname)
    if not t:
        q = asyncio.Queue()
        t = asyncio.create_task(dbtask(dbname, q))
        t.q = q
        dbtasks[dbname] = t
    f = asyncio.Future()
    t.q.put_nowait((query, f))
    result = await f
    return str(result)

<mask token>


async def dbtask(dbname: str, q: asyncio.Queue):
    async with aiosqlite.connect(dbname) as db:
        while True:
            try:
                query, f = q.get_nowait()
            except asyncio.QueueEmpty:
                return
            result = []
            async with db.execute(query) as cursor:
                async for row in cursor:
                    result.append(row)
            await db.commit()
            f.set_result(result)


dbtasks = WeakValueDictionary()


async def run_query(dbname, query: str) ->str:
    t: asyncio.Task = dbtasks.get(dbname)
    if not t:
        q = asyncio.Queue()
        t = asyncio.create_task(dbtask(dbname, q))
        t.q = q
        dbtasks[dbname] = t
    f = asyncio.Future()
    t.q.put_nowait((query, f))
    result = await f
    return str(result)

import asyncio
from weakref import WeakValueDictionary
import aiosqlite


async def dbtask(dbname: str, q: asyncio.Queue):
    async with aiosqlite.connect(dbname) as db:
        while True:
            try:
                query, f = q.get_nowait()
            except asyncio.QueueEmpty:
                return
            result = []
            async with db.execute(query) as cursor:
                async for row in cursor:
                    result.append(row)
            await db.commit()
            f.set_result(result)


dbtasks = WeakValueDictionary()


async def run_query(dbname, query: str) ->str:
    t: asyncio.Task = dbtasks.get(dbname)
    if not t:
        q = asyncio.Queue()
        t = asyncio.create_task(dbtask(dbname, q))
        t.q = q
        dbtasks[dbname] = t
    f = asyncio.Future()
    t.q.put_nowait((query, f))
    result = await f
    return str(result)

import asyncio
from weakref import WeakValueDictionary

import aiosqlite


async def dbtask(dbname: str, q: asyncio.Queue):
    async with aiosqlite.connect(dbname) as db:
        while True:  # Change to a delay-based expiry
            try:
                query, f = q.get_nowait()
            except asyncio.QueueEmpty:
                # The connection will only be kept open while
                # there is work to be done.
                return
            result = []
            async with db.execute(query) as cursor:
                async for row in cursor:
                    result.append(row)
            await db.commit()
            f.set_result(result)


dbtasks = WeakValueDictionary()


async def run_query(dbname, query: str) -> str:  # JSON data
    t: asyncio.Task = dbtasks.get(dbname)
    if not t:
        q = asyncio.Queue()
        t = asyncio.create_task(dbtask(dbname, q))
        t.q = q
        dbtasks[dbname] = t

    f = asyncio.Future()
    t.q.put_nowait((query, f))
    result = await f
    return str(result)
