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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Python and Async Simplified (2018)

73 点作者 dil8超过 2 年前

7 条评论

BiteCode_dev超过 2 年前
It gives good pointers but it falls short on the usual suspects for an article on asyncio.<p>When teaching it, it&#x27;s important to emphasis:<p>- await is locally blocking, so you should isolate linear workflows into their own coro, which is the unit of concurrency.<p>- to allow concurrency, you should use asyncio.create_task on coro (formerly ensure_future).<p>- you should always explicitly delimitate the life cycle of any task. Right now, this means using something like gather() or wait(). TaskGroup will help when it becomes mainstream.<p>A HN comment is not great to explain that, but if you read the article, you should investigate those points. There is no good asyncio code without them, only pain and disapointment.
评论 #33297430 未加载
评论 #33299122 未加载
评论 #33296912 未加载
intrepidhero超过 2 年前
This is the bit that should be at the very top of the official docs. It&#x27;s tripped me up every time I go to write async code and until you learn it, the error message is very confusing.<p>&gt; In particular, calling it will immediately return a coroutine object, which basically says &quot;I can run the coroutine with the arguments you called with and return a result when you await me&quot;.<p>&gt; The code in the target function isn&#x27;t called yet - this is merely a promise that the code will run and you&#x27;ll get a result back, but you need to give it to the event loop to do that.<p>If I try to pass the async function to gather (for example) without calling it, which makes some intuitive sense, since functions are first class objects and I know I&#x27;m not calling it, the event loop is, the error message reads something like, &quot;gather only accepts coroutines.&quot; But I thought it was a coroutine because I declared it with async! For some reason it took me a silly amount of time to notice that in all the examples, the async function is called when it&#x27;s passed to gather (or whatever). That&#x27;s not intuitive to me and the distinction made in the article should be clearer in the docs.
评论 #33303142 未加载
kqr超过 2 年前
Similar question to the other one at the time of writing, but more specific: does anyone have a good, <i>thorough</i> introduction to the &quot;async event loop&quot; (sometimes known as &quot;asyncio&quot;) pattern? By thorough I mean that it goes beyond a starter tutorial, into both examples of various supporting libraries and implementation details that matter for usage. I&#x27;m fine with a book, too.<p>There are popular libraries for it in both Python and Perl and I suspect I could make good use for it if I understood it.<p>Unfortunately, I&#x27;ve only ever used it in a cargo cult manner of sticking together functions until the error messages go away (yeah yeah, it was only for &quot;throwaway&quot; &quot;prototypes&quot;) so I really don&#x27;t understand how it all is meant to fit together.
评论 #33298928 未加载
ogogmad超过 2 年前
I thought Python couldn&#x27;t multithread because of GIL? I understood from the article that async derives all its benefit from certain OS-level operations which don&#x27;t need to run in a coroutine, like reading from a network socket or waiting for timers to finish.<p>Another question: Is Python&#x27;s implementation of async&#x2F;await identical to other languages? In particular, do they always use coroutines instead of threads?
评论 #33297717 未加载
评论 #33297184 未加载
评论 #33299161 未加载
sedeki超过 2 年前
Can anyone recommend a good book&#x2F;primer on &quot;concurrency models&quot; (is that a term?) for a self-taught programmer?<p>While I am self-taught, I&#x27;m used to (academic) books that strive for completeness. It is also what I prefer. Rather than something more pragmatic like a blog post.<p>It doesn&#x27;t mean I want to read overly complicated prose on the subject, which I&#x27;m sure is possible.
评论 #33296786 未加载
评论 #33296941 未加载
评论 #33296883 未加载
landersg超过 2 年前
Async is overengineered and bolted on. If you must use Python, I&#x27;d still recommend Twisted, which is more accessible. Otherwise, of course use Go, Elixir, etc. in the first place.
评论 #33300284 未加载
评论 #33297378 未加载
评论 #33297331 未加载
评论 #33297141 未加载
samsquire超过 2 年前
I am really interested in this space.<p>There&#x27;s an article that Cal Paterson wrote that async doesn&#x27;t speed up code - it is not parallel. The GIL prevents Python from being parallel. So even if you create a thread to run an async method in Python, it shall not run in parallel to the main thread of execution. (In fact, it shall block the main thread of execution if you start a thread in the thread you are in, due to the blocking run_in_executor)<p><a href="https:&#x2F;&#x2F;calpaterson.com&#x2F;async-python-is-not-faster.html" rel="nofollow">https:&#x2F;&#x2F;calpaterson.com&#x2F;async-python-is-not-faster.html</a><p>I wrote a multithreaded userspace 1:M:N scheduler (1 scheduler thread, M kernel threads and N lightweight&#x2F;green threads) which resembles Golang M:N model. I implemented the same design in Rust, C and Java. I am thinking it could be combined with my epoll-server and it would be an application server.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;samsquire&#x2F;preemptible-thread" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;samsquire&#x2F;preemptible-thread</a> <a href="https:&#x2F;&#x2F;github.com&#x2F;samsquire&#x2F;epoll-server" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;samsquire&#x2F;epoll-server</a><p>I am also interested in structured concurrency. This article by Vala developers is good.<p><a href="https:&#x2F;&#x2F;verdagon.dev&#x2F;blog&#x2F;seamless-fearless-structured-concurrency" rel="nofollow">https:&#x2F;&#x2F;verdagon.dev&#x2F;blog&#x2F;seamless-fearless-structured-concu...</a><p>I am trying to find a concurrent software design that is scalable and is easy to write and hides complicated lock programming. I document my studies and ideas in the open in ideas4.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;samsquire&#x2F;ideas4" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;samsquire&#x2F;ideas4</a><p>I&#x27;ve implemented multithreaded parallel multiversion concurrency control in Java, which is the same approach used by Postgresql and MySQL for concurrent read and writing to the same data atomically.<p>I still think concurrency is hard to write and understand. Even with async&#x2F;await.<p>&#x2F;&#x2F; 3 requests in flight<p>result1 = async_task1();<p>result2 = async_task2();<p>result3 = async_task3();<p>await result1;<p>await result2;<p>await result3;<p>I ported a parallel multiconsumer multiproducer ringbuffer from Alek<p><a href="https:&#x2F;&#x2F;www.linuxjournal.com&#x2F;content&#x2F;lock-free-multi-producer-multi-consumer-queue-ring-buffer" rel="nofollow">https:&#x2F;&#x2F;www.linuxjournal.com&#x2F;content&#x2F;lock-free-multi-produce...</a><p>I use Python threads in <a href="https:&#x2F;&#x2F;github.com&#x2F;samsquire&#x2F;devops-schedule" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;samsquire&#x2F;devops-schedule</a> and <a href="https:&#x2F;&#x2F;github.com&#x2F;samsquire&#x2F;parallel-workers" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;samsquire&#x2F;parallel-workers</a> to parallelise a topologically sorted graph of IO of devops programs. This allows efficient scheduling and blocking with thread.join() for each split of the work graph and then a regrouping before doing other things, also potentially in parallel. This pattern is efficient and easy to use.
评论 #33299801 未加载
评论 #33299205 未加载