TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Making 1M requests with Python-aiohttp

123 点作者 dante9999大约 9 年前

11 条评论

terom大约 9 年前
Re the EADDRNOTAVAIL from socket.connect(),<p>If you&#x27;re connecting to 127.0.0.1:8080, then each connection from 127.0.0.1 is going to be assigned an ephemeral TCP source port. There are only a finite number of such ports available, on the order of ~30-50k, which limits the number of connections from a single address to a specific endpoint.<p>If you&#x27;re doing 100k TCP connections with 1k concurrent conections, it&#x27;s feasible that you&#x27;ll run into those limits, with TCP connections hanging around in some TIME_WAIT state after close().<p>Not that this would be a documented errno for connect(), but it&#x27;s the interpretation that makes sense..<p><a href="http:&#x2F;&#x2F;www.toptip.ca&#x2F;2010&#x2F;02&#x2F;linux-eaddrnotavail-address-not.html" rel="nofollow">http:&#x2F;&#x2F;www.toptip.ca&#x2F;2010&#x2F;02&#x2F;linux-eaddrnotavail-address-not...</a> <a href="http:&#x2F;&#x2F;lxr.free-electrons.com&#x2F;source&#x2F;net&#x2F;ipv4&#x2F;inet_hashtables.c?v=4.4#L572" rel="nofollow">http:&#x2F;&#x2F;lxr.free-electrons.com&#x2F;source&#x2F;net&#x2F;ipv4&#x2F;inet_hashtable...</a>
评论 #11558331 未加载
评论 #11559364 未加载
tooker大约 9 年前
I have a library for doing coordinated async IO in python that addresses some of the scheduling and resource contention issues hinted out in the later part of this post. It&#x27;s called cellulario in reference to containing async IO mechanics inside a cell wall..<p><pre><code> https:&#x2F;&#x2F;github.com&#x2F;mayfield&#x2F;cellulario </code></pre> And an example of using it to manage a multi-tiered scheme where a first layer of IO requests seeds another layer and then you finally reduce all the responses..<p><pre><code> https:&#x2F;&#x2F;github.com&#x2F;mayfield&#x2F;ecmcli&#x2F;blob&#x2F;master&#x2F;ecmcli&#x2F;api.py#L456</code></pre>
评论 #11556821 未加载
sandGorgon大约 9 年前
I really keep wishing that there would be benchmark comparisons of asyncio&#x2F;aiohttp with gevent&#x2F;python2 . Performance would be a killer reason to migrate immediately to Py3.<p>What I suspect though is that asyncio is not all that better than gevent. Can someone correct me on this?
评论 #11556474 未加载
velox_io大约 9 年前
The 1 million in the title is misleading (1M per hour is nothing to write home about, only 278&#x2F;sec). There are frameworks that are able hit 1M per minute plus (16,666&#x2F;sec).
评论 #11558195 未加载
评论 #11557378 未加载
评论 #11559618 未加载
ben_jones大约 9 年前
Does anyone enjoy doing async work in python? I&#x27;ve done a few hobby projects and honestly I was yearning for javascript + async lib after awhile. As great as python is maybe we should <i>yield</i> async programming to the languages designed for it?
评论 #11556398 未加载
评论 #11556233 未加载
评论 #11556153 未加载
评论 #11556219 未加载
评论 #11558655 未加载
评论 #11557962 未加载
philippb大约 9 年前
I&#x27;m the CTO at KeepSafe. We open sourced aiohttp.<p>We wrote aiohttp for our production system. We build everything on aiohttp. In our production systems we constantly run more request then in the benchmark with business logic on each request.<p>The main reason we like aiohttp a lot if that you we can write asynchronous code that reads like synchronous and does not have callbacks.
takeda大约 9 年前
IMO you should place all requests within a single ClientSession().<p>This will provide two benefits:<p>1. You won&#x27;t need to use a semaphore. To limit connections you will need to create a TCPConnection() object with limit set to the limit you used in the semaphore and pass it to the ClientSession() and aiohttp will not make more connections than the limit set (default behavior is to have unlimited number of connections).<p>2. With single ClientSession(), aiohttp will make use of keep-alive (i.e. it will reuse same connections for next requests, but it will keep at most the limit of connections you set in TCPConnection() object).<p>This should improve performance further, and (given sane limit) it&#x27;ll also solve issue with &quot;Cannot assign requested address&quot; error.<p>BTW: Even without limit set aiohttp will try to reduce number of connections open so it might still fix the connection error issue as long as individual requests don&#x27;t take long. It&#x27;s still good idea to set limit, just to be nice to the remote server.
nbadg大约 9 年前
First off, awesome to see more benchmarks (even if it&#x27;s just personal experimentation) for synchronous vs asyncio performance. I think the real argument for asyncio <i>right now</i> is that it makes it very easy for you to write extremely efficient code, even for hobbyist projects. Even though your experiment is only handling 320 req&#x2F;s, that you were able to do that so quickly and with very, very little optimization is, I think, a testament to the potential for asyncio.<p>Some pointers:<p>The event loop is still a single thread and therefore subject to the GIL. That means that at any given time, only one coroutine is running in the loop. This is important for several reasons, but probably the most relevant are that<p>1. within any given coroutine, execution flow will always be consistent between yield&#x2F;await statements.<p>2. synchronous calls within coroutines will <i>block the entire event loop</i>.<p>3. most of asyncio was not written with thread safety in mind<p>That second one is really important. When you&#x27;re doing file access, eg where you&#x27;re doing &quot;with open(&#x27;frank.html&#x27;, &#x27;rb&#x27;)&quot;, that&#x27;s something you may want to consider moving into a run_in_executor call. That <i>will</i> block the coroutine, but it will return control to the event loop, allowing other connections to proceed.<p>Also, more likely than not, the too many open files error is a result of you opening frank.html, not of sockets. I haven&#x27;t run your code with asyncio in debug mode[1] to verify that, but that would be my intuition. You would probably handle more requests if you changed that -- I would do the file access in a run_in_executor with a max executor workers of 1000. If you want to surpass that, use a process pool instead of a threadpool, and you should be ready to go, though it&#x27;s worth mentioning that disk IO is hardly ever cpu-bound, so I wouldn&#x27;t expect you to get much performance boost otherwise.<p>Also, the placement of your semaphore acquisition doesn&#x27;t make any sense to me. I would create a dedicated coroutine like this:<p><pre><code> async def bounded_fetch(sem): async with sem: return (await fetch(url.format(i))) </code></pre> and modify the parent function like this:<p><pre><code> for i in range(r): task = asyncio.ensure_future(bounded_fetch(sem)) tasks.append(task) </code></pre> That being said, it also doesn&#x27;t make any sense to me to have the semaphore in the client code, since the error is in the server code.<p>[1] <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;asyncio-dev.html#debug-mode-of-asyncio" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;asyncio-dev.html#debug-mod...</a>
评论 #11558966 未加载
henryw大约 9 年前
Looks pretty interesting to do async on python. I once did something similar in node (async by default) with a few lines of code. I think I scraped 12 or 20 million real URLs in 8 hours on a $5 cloud VM. It was limited by network bandwidth.
azinman2大约 9 年前
&quot;Everyone knows that asynchronous code performs better when applied to network operations&quot;<p>Ummm that seems a bit far reaching.
评论 #11556692 未加载
imaginenore大约 9 年前
1,000,000 requests in 52 minutes is just 320 req&#x2F;sec.<p>Am I missing something? What&#x27;s so amazing about this?<p>I just deployed some production feed that serves at 1955 requests&#x2F;second on a cheap VPS in freaking PHP, one of the slowest languages out there.
评论 #11556498 未加载
评论 #11556413 未加载
评论 #11556293 未加载