Python之Pool

2023-01-31 02:01:56 python pool


#!/usr/bin/env python

from multiprocessing import Pool

import time

def sayHi(a):

        time.sleep(5)

        return a**a

if __name__ =="__main__":

        p=Pool(processes=5)

        result=p.apply_async(sayHi,[10])

        print result.get()

一个池子启用5个线程池,且异步一次返回结果

可以用for循环使用多次异步:

#!/usr/bin/env Python

from multiprocessing import Pool

import time

def sayHi(a):

        time.sleep(5)

        return a**a

p=Pool(processes=5)

result_list=[]

for i in range(100):

        result_list.append(p.apply_async(sayHi,[i]))

for j in result_list:

        print j.get()


相关文章