简体   繁体   中英

For loop multiprocessing in Python

I want to excute for loop on parallel. How can I do that?

for example:

def foo():
    # brahbrah

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

Thank you

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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