Python 线程、队列、异步……这一切意味着什么?
我最近一直在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你想谈论的内容并不是特定于Python的,只是关于多处理与线程一般来说,我认为你可以在谷歌中找到很多来自两侧的争论,即那些更喜欢线程的人和其他人更喜欢多处理的。
但对于 python 多线程来说,(如果您使用的是 CPython)受到 GIL (全局解释器锁),因此大多数 Python 程序员更喜欢使用多处理而不是线程(Guido 建议)
您可以在这里找到一些更多信息
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)
you can find here some more info
当您拥有具有多个内核和/或多 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.