Clickhouse中的异步数据获取不起作用

发布于 2025-01-09 07:09:13 字数 868 浏览 4 评论 0原文

from datetime import datetime

import asyncio
from aiohttp import ClientSession
from aiochclient import ChClient


async def some_query(client, sql):
     print(f"Start {i} - {datetime.now()}")      
     all_rows = await client.fetch(sql, json=True)
     print(f"End {i}")
        
     return all_rows

async def compete():
    tasks = []
    async with ClientSession() as session:
        my_client = ChClient(session, url=f'http://{USER}:{PWD}@{HOST}:{PORT}/', 
                             database=DB)
        for i in range(1, 3):
            task = asyncio.create_task(self.some_query(my_client, i, "SELECT * FROM table"))
            tasks.append(task)
            
        await asyncio.gather(*tasks)

asyncio.run(compete())

该代码可以工作,但是当请求到达数据库时,它会在那里同步执行。 例如,180秒完成3个请求,无论同步还是异步都没有区别。 我是否正确地认为,如果异步运行代码,那么 3 个请求应该在大约 60 秒内完成?

from datetime import datetime

import asyncio
from aiohttp import ClientSession
from aiochclient import ChClient


async def some_query(client, sql):
     print(f"Start {i} - {datetime.now()}")      
     all_rows = await client.fetch(sql, json=True)
     print(f"End {i}")
        
     return all_rows

async def compete():
    tasks = []
    async with ClientSession() as session:
        my_client = ChClient(session, url=f'http://{USER}:{PWD}@{HOST}:{PORT}/', 
                             database=DB)
        for i in range(1, 3):
            task = asyncio.create_task(self.some_query(my_client, i, "SELECT * FROM table"))
            tasks.append(task)
            
        await asyncio.gather(*tasks)

asyncio.run(compete())

The code works, but when the request comes to the database, it is executed there synchronously.
For example, 3 requests are completed in 180 seconds and there is no difference whether it happens synchronously or asynchronously.
Am I right in thinking that if you run the code asynchronously, then 3 requests should be completed in about 60 seconds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文