Python中的For循环多处理

Python中的For循环多处理,python,parallel-processing,Python,Parallel Processing,我想并行执行循环。我该怎么做 例如: def foo(): # brahbrah for i in range(0,5): foo() 谢谢您听说过多线程吗?看看,也许会有帮助。到目前为止你都试过什么?您是否查阅过线程和多处理文档?foo需要共享状态吗? from multiprocessing import Pool from multiprocessing import cpu_count def foo(): # brahbrah def func(cou

我想并行执行循环。我该怎么做

例如:

def foo():
    # brahbrah

for i in range(0,5):
    foo()

谢谢

您听说过多线程吗?看看,也许会有帮助。到目前为止你都试过什么?您是否查阅过线程和多处理文档?foo需要共享状态吗?
from multiprocessing import Pool
from multiprocessing import cpu_count


def foo():
    # brahbrah

def func(count):
    foo()

if __name__ == "__main__":
    pool = Pool(cpu_count())
    count = 5
    results = pool.map(func, range(count))
    pool.close()  # 'TERM'
    pool.join()   # 'KILL'