Skip to content

Instantly share code, notes, and snippets.

@kehao95
Last active June 12, 2018 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kehao95/112bfb5d3406d500cb9d to your computer and use it in GitHub Desktop.
Save kehao95/112bfb5d3406d500cb9d to your computer and use it in GitHub Desktop.
Python 并行化设计

参考parallelism-in-one-line 步骤整理如图下

from multiprocessing import Pool
from multiprocessing.dummy import Pool

简言之,IO 密集型任务选择multiprocessing.dummy,CPU 密集型任务选择multiprocessing ↩ 然后实例化Pool即可使用map

pool = Pool(6)
pool.map()
pool.close()
pool.join()
@kehao95
Copy link
Author

kehao95 commented Aug 5, 2015

一个实际例子

from multiprocessing.dummy import Pool as ThreadPool
import time

class CLS:
    def __init__(self,x):
        pass

    def foo(self):
        print('start')
        time.sleep(1)
        print('end')
        return 

def foo(x):
    x.foo()


l=[0,1,2,3,4,5]
l = map(CLS,l)

pool = ThreadPool(6)
r = pool.map(foo,l)
pool.close()
pool.join()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment