Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在200000个值的数组上运行多处理?_Python_Multithreading_Concurrency - Fatal编程技术网

Python 如何在200000个值的数组上运行多处理?

Python 如何在200000个值的数组上运行多处理?,python,multithreading,concurrency,Python,Multithreading,Concurrency,假设我有和数组,长度为200000。我想对该数组的每一个值运行一个函数,我必须使用ThreadPoolExecutor来执行。我怎么能这样做 allIds = [...] #Len = 200000 def someFunc(id): [...] for id in allIds: cur = int(id) someFunc(id = cur) 我怎么能做同样的事情,但更快 import time import concurrent.futures as cf allId

假设我有和数组,长度为200000。我想对该数组的每一个值运行一个函数,我必须使用ThreadPoolExecutor来执行。我怎么能这样做

allIds = [...] #Len = 200000

def someFunc(id):
[...]

for id in allIds:
    cur = int(id)
    someFunc(id = cur)
我怎么能做同样的事情,但更快

import time
import concurrent.futures as cf
allIds = [1,2,3,4,5,6,7,8,9,10] #Len = 200000

def someFunc(idd):
    time.sleep(3)
    return idd*2

with cf.ThreadPoolExecutor() as executor:
        future_to_mapping = [executor.submit(someFunc, i) for i in allIds]

for future in cf.as_completed(future_to_mapping):
    print(future.result())

如果max_workers没有或没有给定,它将默认为机器上的处理器数量乘以5,您知道
多线程
(即使用您在正文中提到的
ThreadPoolExecutor
,…我必须使用ThreadPoolExecutor…)以及您在标题中提到的
多处理
(在这种情况下,您可以使用?