concurrent.futures模块提供了高度封装的异步调用接口
ThreadPoolExecutor:线程池,提供异步调用
ProcessPoolExecutor: 进程池,提供异步调用
submit(fn, *args, **kwargs)异步提交任务
shutdown(wait=True) 相当于进程池的pool.close()+pool.join()操作
import os
import random
import time
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
def task(name):
print('name:%s pid:%s run' % (name, os.getpid()))
time.sleep(random.randint(1, 3))
if __name__ == '__main__':
pool = ProcessPoolExecutor(4)
# pool = ThreadPoolExecutor(5)
for i in range(10):
pool.submit(task, 'winnie%s' % i)
pool.shutdown(wait=True)
print('主')
import os
import random
import time
from concurrent.futures import ThreadPoolExecutor
from threading import currentThread
def task():
print('name:%s pid:%s run' % (currentThread().getName(), os.getpid()))
time.sleep(random.randint(1, 3))
if __name__ == '__main__':
pool = ThreadPoolExecutor(5)
for i in range(10):
pool.submit(task, )
pool.shutdown(wait=True)
print('主')
使用异步调用机制:提交完任务后,不用等待任务执行完毕
import time
from concurrent.futures import ThreadPoolExecutor
import requests
def get(url):
print('GET %s' % url)
response = requests.get(url)
time.sleep(3)
return {'url': url, 'content': response.text}
def parse(res):
res = res.result()
print('%s parse res is %s' % (res['url'], len(res['content'])))
if __name__ == '__main__':
urls = [
'http://www.cnblogs.com/linhaifeng',
'https://www.python.org',
'https://www.openstack.org',
]
pool = ThreadPoolExecutor(2)
for url in urls:
pool.submit(get, url).add_done_callback(parse)