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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Python Concurrency: Threads, Processes, and Asyncio Explained

89 点作者 BerislavLopac4 个月前

10 条评论

est4 个月前
While these kind of articles are useful for learners, I hope someone please explain concurrency models for uWSGI&#x2F;Gunicorn&#x2F;uvicorn&#x2F;gevent. Like how long does global variables live? How does context switching (like the magic request from Flask) work? How to spawn async background tasks? Is it safe to mix task schedulers inside web code? How to measure when concurrency is full and how to scale? What data can be shared between executors and how? How to detect back-pressure? How to interrupt long-running function when client disconnects (nginx 499)? How to proper handle unix signals for threads&#x2F;multiprocess&#x2F;asyncio?<p>I reality no one writes from scratch with threads, processes or asyncio unless you are a library author.
评论 #42648223 未加载
WoodenChair4 个月前
Unfortunately this starts with a definition of concurrency quoted from the Python wiki [0] which is imprecise: &quot;Concurrency in programming means that multiple computations happen at the same time.&quot;<p>Not necessarily. It means multiple computations <i>could</i> happen at the same time. Wikipedia has a broader definition [1]: &quot;Concurrency refers to the ability of a system to execute multiple tasks through simultaneous execution or time-sharing (context switching), sharing resources and managing interactions.&quot;<p>In other words it could be at the same time or it could be context switching (quickly changing from one to another). Parallel [2] means explicitly at the same time: &quot;Parallel computing is a type of computation in which many calculations or processes are carried out simultaneously.&quot;<p>0: <a href="https:&#x2F;&#x2F;wiki.python.org&#x2F;moin&#x2F;Concurrency" rel="nofollow">https:&#x2F;&#x2F;wiki.python.org&#x2F;moin&#x2F;Concurrency</a><p>1: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Concurrency_(computer_science)" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Concurrency_(computer_science)</a><p>2: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Parallel_computing" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Parallel_computing</a>
评论 #42641161 未加载
评论 #42637842 未加载
whilenot-dev4 个月前
&gt; Summary<p><pre><code> if cpu_intensive: &#x27;processes&#x27; else: if suited_for_threads: &#x27;threads&#x27; elif suited_for_asyncio: &#x27;asyncio&#x27; </code></pre> Interesting takeaway! For web services mine would be:<p>1. always use <i>asyncio</i><p>2. use threads with <i>asyncio.to_thread</i>[0] to convert blocking calls to asyncio with ease<p>3. use <i>aiomultiprocess</i>[1] for any cpu intensive tasks<p>[0]: <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;asyncio-task.html#asyncio.to_thread" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;asyncio-task.html#asyncio....</a><p>[1]: <a href="https:&#x2F;&#x2F;aiomultiprocess.omnilib.dev&#x2F;en&#x2F;stable&#x2F;" rel="nofollow">https:&#x2F;&#x2F;aiomultiprocess.omnilib.dev&#x2F;en&#x2F;stable&#x2F;</a>
评论 #42633373 未加载
daelon4 个月前
Don&#x27;t know if the author will show up here, but the code highlighting theme is almost unreadable, at least on chrome on android.
评论 #42639682 未加载
评论 #42631346 未加载
tomtom13374 个月前
I would have liked to see the performance of the ash c version, seems like a surprising omission.<p>I’m also confused about the perf2 performance. For the threads example it starts around 70_000 reqs&#x2F;sec, while the processes example runs at 3_500 reqs&#x2F;sec. That’s a 20 times difference that isn’t mentioned in the text.
t_mahmood4 个月前
Wondering anyone have multiprocessing freezing on Windows? As it seems you need to have __main__ for multiprocessing to work in Windows, which I do not have as I&#x27;m using pyproject scripts with click to run. Have anyone face these issue? Is there any solution for this issue?
评论 #42636992 未加载
captaindiego4 个月前
Any advice for debugging asyncio? I&#x27;ve tried it a few times but every time it got a bit more complicated it felt very hard to figure out what was going wrong.
评论 #42648316 未加载
timonoko4 个月前
Grok made better job of explaining different solutions in micropython environment. Summary:<p>* Task Parallelism and Multi-threading are good for computational tasks spread across the ESP32&#x27;s dual cores.<p>* Asynchronous Programming shines in scenarios where I&#x2F;O operations are predominant.<p>* Hardware Parallelism via RMT can offload tasks from the CPU, enhancing overall efficiency for specific types of applications.
评论 #42637001 未加载
griomnib4 个月前
The real fun is when you have multiple processes also spawning threads.
c-fe4 个月前
Not really anything new in there. Been dealing with python concurrency a lot and i dont find it great compared to other languages (eg kotlin).<p>One thing I am struggling with right now is how do I handle a function that its both I&#x2F;O intensive and CPU-bound? To give more context, I am processing data which on paper is easy to parallelise. Say for 1000 lines of data, I have to execute my function f for each line, in any order. However f using the cpu a lot, but also doing up to 4 network requests.<p>My current approach is to divide 1000&#x2F;n_cores, then launch n_cores processes and on each of them run the function f asynchronoulsy on all inputs of that process, async to handle switching on I&#x2F;O. I wonder if my approach could be improved.
评论 #42632750 未加载
评论 #42638104 未加载