multiprocessing包是Python中的多进程管理包.
Pool(num)类提供一个进程池,然后在多个核中执行这些进程,
其中默认参数num是当前机器CPU的核数.
Pool.map(func, iterable[, chunksize=None])
2个参数, 第一个参数是函数, 第二个参数是需要可迭代的变量, 作为参数传递到func
如果func含有的参数多于一个,可以利用functools.partial 先处理.
以下是一个简单的例子.
from multiprocessing import Pool from functools import partial def somefunc(str_1, str_2, iterable_iterm): print("%s %s %d" % (str_1, str_2, iterable_iterm)) def main(): iterable = [1, 2, 3, 4, 5] pool = Pool() str_1 = "This" str_2 = "is" func = partial(somefunc, str_1, str_2) pool.map(func, iterable) pool.close() pool.join() if __name__ == "__main__": main()
原文出处:csdn -> http://blog.csdn.net/noirblack/article/details/79068068