多进程:Python中提供multiprocess模块实现多进程并发
import multiprocessing
import time
def worker():
number = 0
for i in range(10000000):
number += 1
time.sleep(0.1)
print(multiprocessing.current_process().name) # 获取当前进程的名称
print('循环次数:{} number:{}'.format(i+1,number ))
if __name__ == '__main__':
for i in range(4):
process = multiprocessing.Process(target=worker, name='process worker {}'.format(i))
process.start()
print(process.pid) # 获取进程的pid
print(process.exitcode) # 获取退出状态码
print(process.name) # 获取进程名称
time.sleep(30)
print(process.terminate()) # 终止进程
进程并发:concurrent模块
虽然multiprocess模块有一个Pool类,但是一般而言,用的多的还是ProcessPoolExecutor,异步进程池
pool的对象方法
①:submit()
②:map()
③:shutdown(): 清理进程池
submit()方法的返回值future对象的方法
①:result():查看调用的返回结果
②:done():如果任务成功执行或任务取消返回True
③:cancel():取消任务
④:running(): 如果任务正在执行返回True
⑤:exception(): 获取执行抛出的异常
from concurrent.futures import ProcessPoolExecutor
import requests
def fetch_url(url):
result = requests.get(url=url, )
return result.text
if __name__ == '__main__':
# 创建10个线程队列的线程池
pool = ProcessPoolExecutor(10)
# 获取任务返回对象
a = pool.submit(fetch_url, 'http://www.yuxiaokeji.cn')
b = pool.submit(fetch_url, 'http://www.yuxiaokeji.cn')
# 取出返回的结果
x = a.result()
y = b.result()
print(x)
print(y)
使用上下文管理
if __name__ == '__main__':
with ProcessPoolExecutor(max_workers=10) as pool:
a = pool.submit(fetch_url, 'http://www.yuxiaokeji.cn')
print(a.result())
网友评论