Python 中的简单多线程 for 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38856172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:31:25  来源:igfitidea点击:

Simple multithread for loop in Python

pythonmultithreadingiteration

提问by Anthony

I searched everywhere and I don't find any simple example of iterating a loop with multithreading.

我到处搜索,但没有找到任何使用多线程迭代循环的简单示例。

For example, how can I multithread this loop?

例如,我如何多线程这个循环?

for item in range(0, 1000):
    print item

Is there any way to cut it in like 4 threads, so each thread has 250 iterations?

有没有办法把它切成 4 个线程,所以每个线程有 250 次迭代?

采纳答案by janbrohl

Easiest way is with multiprocessing.dummy(which uses threads instead of processes) and a Pool

最简单的方法是使用multiprocessing.dummy(它使用线程而不是进程)和一个Pool

import multiprocessing.dummy as mp 

def do_print(s):
    print s

if __name__=="__main__":
    p=mp.Pool(4)
    p.map(do_print,range(0,10)) # range(0,1000) if you want to replicate your example
    p.close()
    p.join()

Maybe you want to try real multiprocessing, too if you want to better utilize multiple CPUs but there are several caveats and guidelinesto follow then.

如果您想更好地利用多个 CPU,也许您也想尝试真正的多处理,但有几个注意事项和指导原则需要遵循。

Possibly other methods of Poolwould better suit your needs - depending on what you are actually trying to do.

可能其他方法Pool更适合您的需求 - 取决于您实际尝试做什么。

回答by ForceBru

You'll have to do the splitting manually:

您必须手动进行拆分:

import threading

def ThFun(start, stop):
    for item in range(start, stop):
        print item

for n in range(0, 1000, 100):
    stop = n + 100 if n + 100 <= 1000 else 1000
    threading.Thread(target = ThFun, args = (n, stop)).start()

This code uses multithreading, which means that everything will be run within a single Python process (i.e. only one Python interpreter will be launched).

此代码使用多线程,这意味着一切都将在单个 Python 进程中运行(即仅启动一个 Python 解释器)。

Multiprocessing, discussed in the other answer, means running some code in several Python interpreters(in several processes, not threads). This may make use of all the CPU cores available, so this is useful when you're focusing on the speed of your code (print a ton of numbers until the terminal hates you!), not simply on parallel processing. 1

另一个答案中讨论的多处理意味着在多个 Python 解释器中运行一些代码(在多个进程中,而不是线程中)。这可能会利用所有可用的 CPU 内核,因此当您关注代码的速度时(打印大量数字直到终端讨厌您!),而不仅仅是并行处理,这很有用。1



1. multiprocessing.dummyturns out to be a wrapper around the threadingmodule. multiprocessingand multiprocessing.dummyhave the same interface, but the first module does parallel processing using processes, while the latter - using threads.

1.multiprocessing.dummy原来threading模块的包装器multiprocessingmultiprocessing.dummy具有相同的接口,但第一个模块使用进程进行并行处理,而后者使用线程