python处理cpu密集型任务时,用多线程会好点
from multiprocessing import Pool, Manager
import os, time, random
n = 100
def write(q):
print('Process to write: {}'.format(os.getpid()))
time.sleep(random.random())
q.put(0)
print('{}/{}'.format(q.qsize(), n))
if __name__=='__main__':
p = Pool(8)
q = Manager().Queue()
for i in range(0, n):
p.apply_async(write, args=(q, ))
p.close()
p.join()