Clickhouse中的异步数据获取不起作用
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论