进程:内存空间隔离
线程:内存资源共享
多进程(并发)
from multiprocessing import *
import time
def task(name):
print('hahah %s'%name)
time.sleep(2)
print('hehee %s'%name)
if __name__ == '__main__':
p = Process(target=task, args=('sunv',))
p.start()
print('zhu')
并发:就是空闲时运行其他任务,只是切换运行时我们感受不到
并行:相当于几个cpu真的分别运行任务
开进程的两种方式:
方式二:(了解)方式一: from multiprocessing import Process import time def task(name): print('%s is running' %name) time.sleep(2) if __name__ == '__main__': #在windows系统下,开子进程的代码必须写到这一行下面 p=Process(target=task,args=('egon',)) p.start() #只是在给操作系统发了一个信号,让操作系统去开进程(申请内存+拷贝父进程的地址空间) print('主')
进程池:from multiprocessing import Process import time class Myprocess(Process): def __init__(self,name): super().__init__() self.name=name def run(self): time.sleep(3) print('%s is running' % self.name) time.sleep(2) if __name__ == '__main__': # 在windows系统下,开子进程的代码必须写到这一行下面 p = Myprocess('egon') p.start() # p.run() print('主')
提交/调用任务的方式有两种:
同步调用:提交/调用一个任务,然后就在原地等着,等到该任务执行完毕拿到结果,再执行下一行代码
异步调用: 提交/调用一个任务,不在原地等着,直接执行下一行代码,结果?补充异步概念:# from multiprocessing import Process,Pool from concurrent.futures import ProcessPoolExecutor import time,random,os def piao(name,n): print('%s is piaoing %s' %(name,os.getpid())) time.sleep(1) return n**2 if __name__ == '__main__': p=ProcessPoolExecutor(4) objs=[] start=time.time() for i in range(10): # res=p.submit(piao,'alex %s' %i,i).result() #同步调用# # print(res) obj=p.submit(piao,'alex %s' %i,i) #异步调用 objs.append(obj) for obj in objs: print(obj.result()) stop=time.time() print(stop-start) # 关门+等# # pool.close()# # pool.join() p.shutdown(wait=True) print('主',os.getpid())
进程池:回调的活是 主进程干:同步方式(不重要) import requests from threading import current_thread urls = [ 'https://www.python.org' 'https://www.baidu.com' 'https://www.jd.com' 'https://www.tmall.com' ] def get(url): print('%s GET %s'%(current_thread().getName(),url)) response = requests.get(url) if response.status_code == 200: return {'url':url,'text':response.text} def parse(res): # print('%s parse %s' % (current_thread().getName(), res['url'])) # print('[%s] parse res [%s]'%(res['url'],len(res['text']))) print('[%s] <%s> (%s)' % (os.getpid(), res['url'],len(res['text']))) for url in urls: res = get(url) parse(res)
异步调用:import requests from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor from threading import current_thread import time import os def get(url): print('%s GET %s' %(os.getpid(),url)) response=requests.get(url) time.sleep(3) if response.status_code == 200: return {'url':url,'text':response.text} def parse(obj): res=obj.result() print('[%s] <%s> (%s)' % (os.getpid(), res['url'],len(res['text']))) if __name__ == '__main__': urls = [ 'https://www.python.org', 'https://www.baidu.com', 'https://www.jd.com', 'https://www.tmall.com', ] t=ProcessPoolExecutor(2) for url in urls: t.submit(get,url).add_done_callback(parse) t.shutdown(wait=True) print('主',os.getpid())
提交完任务(为该任务绑定一个回调函数),不用再原地等任务执行完毕拿到结果,可以直接提交下一个任务
一个任务一旦执行完毕就会自动触发回调函数的运行
回调函数的参数是单一的:
回调函数的参数就是它所绑定任务的返回值
线程池:回调的活,大家一起干,谁有空谁干:pip install requests import requests,time,os from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor from threading import current_thread def get(url): print('%s GET %s' %(current_thread().getName(),url)) response=requests.get(url) time.sleep(3) if response.status_code == 200: return {'url':url,'text':response.text} def parse(obj): res=obj.result() print('[%s] <%s> (%s)' % (current_thread().getName(), res['url'],len(res['text']))) if __name__ == '__main__': urls = [# 'https://www.python.org', 'https://www.baidu.com', 'https://www.jd.com', 'https://www.tmall.com',# ] t=ThreadPoolExecutor(2) for url in urls: t.submit(get,url).add_done_callback(parse) t.shutdown(wait=True) print('主',os.getpid())
秋酿 --- 房东的猫