Python 线程、队列、异步……这一切意味着什么?

发布于 2024-09-30 23:22:12 字数 255 浏览 2 评论 0原文

我最近一直在 Python 中尝试线程,并且很好奇何时使用什么。

例如,什么时候应该使用多线程而不是多处理?当我应该使用异步 IO 而不是线程时,会出现什么情况?

我主要了解每种方法的作用(我认为),但我看不出使用一种方法比另一种方法有任何好处/缺点。

  • 如果我要创建一个小型 HTTP 服务器,我应该使用什么?
  • 如果我要创建一个小型 HTTP 客户端,我应该使用什么?

这让我很困惑...

I've been experimenting with threading recently in Python and was curious when to use what.

For example, when should I use multithreading over multiprocessing? What would be a scenario when I should be using asynchronous IO rather than threading?

I mostly understand what each does (I think) but I can't see any benefits/downsides of using one over the other.

  • What should I use if I was creating a small HTTP server?
  • What should I use if I was creating a small HTTP client?

This baffles me...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空袭的梦i 2024-10-07 23:22:12

你想谈论的内容并不是特定于Python的,只是关于多处理与线程一般来说,我认为你可以在谷歌中找到很多来自两侧的争论,即那些更喜欢线程的人和其他人更喜欢多处理的。

但对于 python 多线程来说,(如果您使用的是 CPython)受到 GIL (全局解释器锁),因此大多数 Python 程序员更喜欢使用多处理而不是线程(Guido 建议)

尽管如此,你是对的,GIL 是
不像你最初想象的那么糟糕
认为:你只需撤消
Windows 给你带来的洗脑
Java 支持者们似乎在考虑
线程作为唯一的方法
同期活动,Guido van Rossum。

您可以在这里找到一些更多信息

What you want talk about is not specific to python only it's about multiprocessing vs threading in general i think you can find in google lot of argument from the two side the ones that prefer threading and the others that prefer multiprocessing.

But for python multi-threading is limited (if you're using CPython) by the GIL (Global Interpreter Lock), so most python programmer prefer using the multiprocessing over the threading (it's Guido recommendation)

Nevertheless, you re right the GIL is
not as bad as you would initially
think: you just have to undo the
brainwashing you got from Windows and
Java proponents who seem to consider
threads as the only way to approach
concurrent activities, Guido van Rossum.

you can find here some more info

呆° 2024-10-07 23:22:12

当您拥有具有多个内核和/或多 CPU 的机器时,Python 多处理就有意义。使用线程和进程之间的主要区别在于进程不共享地址空间,因此一个进程无法轻松访问另一个进程的数据。这就是为什么多处理模块提供管理器和队列之类的东西。

线程的问题是 Python 的全局解释器锁,它严重扰乱了多线程应用程序。

当您长时间运行 IO 操作(读取大文件、等待网络响应)并且不希望应用程序阻塞时,异步 IO 非常有用。许多操作系统都提供了该功能的内置实现。

因此,对于您的服务器,您可能会使用多处理或多线程,而对于您的客户端,异步 IO 更合适。

Python multiprocessing makes sense when you have a machine with multiple cores and/or CPUs. The main difference between using threads and processes is that processes do not share an address space, and thus one process cannot easily access the data of another process. That is why the multiprocessing module provides managers and queues and stuff like that.

The issue with threading is Pythons Global Interpreter Lock, which seriously messes with multithreaded applications.

Asynchronous IO is useful when you have long running IO operations (read large file, wait for response from network) and do not want your application to block. Many operating systems offer built-in implementations of that.

So, for your server you would probably use multiprocessing or multithreading, and for your client async IO is more fitting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文