TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Python3 Asyncio for middling developers

35 pointsby coffeefirstover 7 years ago

6 comments

lilbobbytablesover 7 years ago
&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 未加载
Twirrimover 7 years ago
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 未加载
FridgeSealover 7 years ago
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_over 7 years ago
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.
agumonkeyover 7 years ago
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
setrover 7 years ago
this is actually titled &quot;I’m too stupid for AsyncIO&quot;<p>and its about NOT understanding AsyncIO.
评论 #15166931 未加载