【发布时间】:2021-07-06 12:30:50
【问题描述】:
我有兴趣为异步函数调用创建一个池(它们将是 HTTP 请求),但是我想在一个线程中完成所有事情。这样做的原因是产生多个线程会浪费资源(线程什么都不做,只是等待响应)。
import asyncio
import aiohttp
import some_library as pool
POOL_LIMIT = 3
urls = ["example.com/28409078",
"example.com/31145880",
"example.com/54622752",
"example.com/48008963",
"example.com/82016326",
"example.com/75587921",
"example.com/2988065",
"example.com/47574087",
"example.com/13478021",
"example.com/46041669"]
def get(url):
# return some promise here
# now perform the async operations
pool(limit=POOL_LIMIT, urls, get)
是否有可以为我管理异步池的 python 库?在 Node.js 中,看起来有一个库可以做一些接近我想做的事情:https://github.com/rxaviers/async-pool
【问题讨论】:
-
您能说说您使用的是哪个库吗?
-
如果有帮助,我正在使用
asyncio和aiohttp -
asyncio库在设计上是单线程的。如果你想限制并发使用semaphore。另请注意,默认情况下,aiohttp 的客户端会话允许 100 个并发连接。更多详情请见docs.aiohttp.org/en/stable/…
标签: python asynchronous web-scraping