参考资料地址:https://www.cnblogs.com/fugeny/p/9898971.html#%E5%B9%B6%E8%A1%8C%E8%AE%A1%E7%AE%97
from multiprocessing import Pool
import time
#下面是例子内容,我们只需要在此基础上修改即可
#在这里我们定义自己的函数,也就是说输入是MP3
文件或者是text,然后输出是MP3文件的长度
def f(x):
return x*x
#主程序
if __name__ == '__main__':
#这里就是processes=核数
pool = Pool(processes=4) # start 4 worker processes
#apply_async好处是不会阻塞进程,参数1就是目标函数名,参数2是函数所接受的参数
result = pool.apply_async(f, (10,)) # evaluate "f(10)" asynchronously in a single process
#获取单个输出
print result.get(timeout=1) # prints "100" unless your computer is *very* slow
#映射版本的输出,这个比较适合多个采纳数的时候使用
print pool.map(f, range(10)) # prints "[0, 1, 4,..., 81]"
pool.close()
pool.join()
#迭代器版本
#it = pool.imap(f, range(10))
#print it.next() # prints "0"
#print it.next() # prints "1"
#print it.next(timeout=1) # prints "4" unless your computer is *very* slow
result = pool.apply_async(time.sleep, (10,))
print result.get(timeout=1) # raises multiprocessing.TimeoutError
如果结果不正常,我们需要ctrl+c终止程序
参考资料2地址:https://www.cnblogs.com/zz27zz/p/7886146.html
import time
import threadpool
#定义执行函数
def sayhello(str):
print "Hello ",str
time.sleep(2)
#执行函数输入的参数列表
name_list =['xiaozi','aa','bb','cc']
#测试时间的time模块可以去除
start_time = time.time()
pool = threadpool.ThreadPool(10) #此处的10是poolsize 一般也就是cpu数量
requests = threadpool.makeRequests(sayhello, name_list) #导入函数和函数参数
[pool.putRequest(req) for req in requests]
pool.wait()
print '%d second'% (time.time()-start_time)