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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Python3 Asyncio for middling developers

35 点作者 coffeefirst超过 7 年前

6 条评论

lilbobbytables超过 7 年前
&gt; That seems wrong. I should be able to run normal Python code as async, right?<p>No, that is not the case. To better understand this you should look at an async library, like <a href="https:&#x2F;&#x2F;github.com&#x2F;aio-libs&#x2F;aiohttp" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;aio-libs&#x2F;aiohttp</a> Look at what it actually calls all the way down under the hood.<p>If it were as simple as adding `asyncio.sleep(0)`, then that library seems as though it would have been much easier to write. :P<p>Just look at the code you posted at the end, it actually runs faster synchronously, without `asyncio.sleep(0)`. The sleep is what happens async, not the print statements, therefore, all you&#x27;re doing is introducing delay.<p>Similarly, the Django ORM DB calls you make in the other examples are all still happening synchronously. However, you&#x27;re just adding a delay that causes them to get picked off in an inconsistent order.
评论 #15167182 未加载
Twirrim超过 7 年前
For OP&#x27;s case, I wouldn&#x27;t have jumped to async, but instead either to multithreading or multiprocessing. Pool().map makes this really trivial. Taking their example, and tweaking it slightly:<p><pre><code> import requests from multiprocessing import Pool def fetch_things(): pool = Pool() # defaults to number of CPUs urls = [&#x27;https:&#x2F;&#x2F;example.com&#x2F;api&#x2F;object&#x2F;1&#x27;, &#x27;https:&#x2F;&#x2F;example.com&#x2F;api&#x2F;object&#x2F;2&#x27;, &#x27;https:&#x2F;&#x2F;example.com&#x2F;api&#x2F;object&#x2F;3&#x27;] return pool.map(requests.get, urls) print(fetch_things()) </code></pre> Output (because those URLs are nonsense...):<p><pre><code> [&lt;Response [404]&gt;, &lt;Response [404]&gt;, &lt;Response [404]&gt;] </code></pre> It&#x27;s just as easy to do it in threading. Just switch that &quot;from multiprocessing import Pool&quot; with &quot;from multiprocessing.dummy import Pool&quot;
评论 #15170298 未加载
FridgeSeal超过 7 年前
I&#x27;m sure someone will correct me here, but in my experience, Pythons Async model is a cluster-fuck.<p>It feels very much like it was just kind of thrown in to keep up with the trends, without any thought as to whether it made sense or whether it was the most &quot;Pythonic&quot; way of implementing it.
评论 #15170829 未加载
_pmf_超过 7 年前
Async IO libraries tend to make very simple things very complicated. I spent half a day trying to use Boost.Asio to receive a UDP frame before giving up and using QUdpSocket (which took less that 5 minutes).<p>Agreed with the author&#x27;s sentiment of feeling stupid.
agumonkey超过 7 年前
coroutines are tacos, and monads are burritos. Repeat after me.<p>ps: for the author, my only theory about the 0s sleep is that coroutines aren&#x27;t preempted like threads, they use collaborative concurrency, so unless they actually say &quot;ok I agree to pause now and let others do something&quot; well the interpreter will evaluate all the instructions until completion. My 2 cents
setr超过 7 年前
this is actually titled &quot;I’m too stupid for AsyncIO&quot;<p>and its about NOT understanding AsyncIO.
评论 #15166931 未加载