简体   繁体   English

我应该对 I/O 和 CPU 绑定的任务使用多线程吗?

[英]Should I use multithreading for a task that is both I/O and CPU bound?

There is a lot of information on the web about Python's multi-threading and multiprocessing modules and why someone should use one over the another. web 上有很多关于 Python 的多线程和多处理模块以及为什么有人应该使用其中一个模块的信息。 The basic knowledge is that multi-threading is ideal for I/O bound tasks and multiprocessing for CPU bound tasks.基本知识是多线程非常适合 I/O 绑定任务和多处理 CPU 绑定任务。 All good so far.到目前为止一切都很好。

But real-life scenarios are often not that clear and often you are faced with situations when the underlying task is a mix of both I/O and CPU bound work.但是现实生活中的场景通常并不那么清晰,而且您经常会遇到底层任务是 I/O 和 CPU 密集型工作的混合情况。 I'm currently facing a scenario where my task would be spending 80% of its time waiting for an API call that is essentially an I/O bound task but, it will spend 20% of the remaining time in processing the data from that API.我目前面临的情况是,我的任务将花费 80% 的时间等待 API 调用,该调用本质上是一个 I/O 绑定任务,但是它将花费剩余时间的 20% 来处理来自该 API 的数据. This makes my task mixed with I/O and CPU bound work.这使我的任务与 I/O 和 CPU 密集型工作混合在一起。

My question is that how should I decide the ideal module to be used for such scenarios?我的问题是,我应该如何决定用于此类场景的理想模块? Should I also use a mixed model where the I/O bound work is done by multi-threading and vice versa?我是否还应该使用混合 model,其中 I/O 绑定工作由多线程完成,反之亦然?

There is a lot of information on the web about Python's multi-threading and multiprocessing modules and why someone should use one over the another. web 上有很多关于 Python 的多线程和多处理模块以及为什么有人应该使用其中一个模块的信息。 The basic knowledge is that multi-threading is ideal for I/O bound tasks and multiprocessing for CPU bound tasks.基本知识是多线程非常适合 I/O 绑定任务和多处理 CPU 绑定任务。 All good so far.到目前为止一切都很好。

But real-life scenarios are often not that clear and often you are faced with situations when the underlying task is a mix of both I/O and CPU bound work.但是现实生活中的场景通常并不那么清晰,而且您经常会遇到底层任务是 I/O 和 CPU 密集型工作的混合情况。 I'm currently facing a scenario where my task would be spending 80% of its time waiting for an API call that is essentially an I/O bound task but, it will spend 20% of the remaining time in processing the data from that API.我目前面临的情况是,我的任务将花费 80% 的时间等待 API 调用,该调用本质上是一个 I/O 绑定任务,但是它将花费剩余时间的 20% 来处理来自该 API 的数据. This makes my task mixed with I/O and CPU bound work.这使我的任务与 I/O 和 CPU 密集型工作混合在一起。

My question is that how should I decide the ideal module to be used for such scenarios?我的问题是,我应该如何决定用于此类场景的理想模块? Should I also use a mixed model where the I/O bound work is done by multi-threading and vice versa?我是否还应该使用混合 model,其中 I/O 绑定工作由多线程完成,反之亦然?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM