from time import sleep
import multiprocessing
def get_html(t):
sleep(t)
print('{}执行可以'.format(t))
return t
if __name__ == '__main__':
# 通过 cpu_count() 获取当前主机的核心数
pool = multiprocessing.Pool(multiprocessing.cpu_count())
result = pool.apply_async(get_html,args=(3,)) # 参数是元组
pool.close() # 必须先 调用 close 才可以 join
pool.join()
print(result.get())
print('end')
map 方法
from time import sleep
import multiprocessing
def get_html(t):
sleep(t)
print('{}执行可以'.format(t))
return t
if __name__ == '__main__':
# 通过 cpu_count() 获取当前主机的核心数
pool = multiprocessing.Pool(multiprocessing.cpu_count())
for i in pool.imap(get_html,[4,2,3]):
# for i in pool.imap_unordered(get_html,[4,2,3]):
print(i,'返回值')