回调函数 女爷i 2022-01-07 07:41 303阅读 0赞 **需要回调函数的场景:进程池中任何一个任务一旦处理完了,就立即告知主进程:我好了额,你可以处理我的结果了。主进程则调用一个函数去处理该结果,该函数即回调函数** **我们可以把耗时间(阻塞)的任务放到进程池中,然后指定回调函数(主进程负责执行),这样主进程在执行回调函数时就省去了I/O的过程,直接拿到的是任务的结果。** from multiprocessing import Pool import requests import json import os def get_page(url): print('<进程%s> get %s' %(os.getpid(),url)) respone=requests.get(url) if respone.status_code == 200: return { 'url':url,'text':respone.text} def pasrse_page(res): print('<进程%s> parse %s' %(os.getpid(),res['url'])) parse_res='url:<%s> size:[%s]\n' %(res['url'],len(res['text'])) with open('db.txt','a') as f: f.write(parse_res) if __name__ == '__main__': urls=[ 'https://www.baidu.com', 'https://www.python.org', 'https://www.openstack.org', 'https://help.github.com/', 'http://www.sina.com.cn/' ] p=Pool(3) res_l=[] for url in urls: res=p.apply_async(get_page,args=(url,),callback=pasrse_page) res_l.append(res) p.close() p.join() print([res.get() for res in res_l]) #拿到的是get_page的结果,其实完全没必要拿该结果,该结果已经传给回调函数处理了 ''' 打印结果: <进程3388> get https://www.baidu.com <进程3389> get https://www.python.org <进程3390> get https://www.openstack.org <进程3388> get https://help.github.com/ <进程3387> parse https://www.baidu.com <进程3389> get http://www.sina.com.cn/ <进程3387> parse https://www.python.org <进程3387> parse https://help.github.com/ <进程3387> parse http://www.sina.com.cn/ <进程3387> parse https://www.openstack.org [{'url': 'https://www.baidu.com', 'text': '<!DOCTYPE html>\r\n...',...}] ''' 爬虫案例 from multiprocessing import Pool import time,random import requests import re def get_page(url,pattern): response=requests.get(url) if response.status_code == 200: return (response.text,pattern) def parse_page(info): page_content,pattern=info res=re.findall(pattern,page_content) for item in res: dic={ 'index':item[0], 'title':item[1], 'actor':item[2].strip()[3:], 'time':item[3][5:], 'score':item[4]+item[5] } print(dic) if __name__ == '__main__': pattern1=re.compile(r'<dd>.*?board-index.*?>(\d+)<.*?title="(.*?)".*?star.*?>(.*?)<.*?releasetime.*?>(.*?)<.*?integer.*?>(.*?)<.*?fraction.*?>(.*?)<',re.S) url_dic={ 'http://maoyan.com/board/7':pattern1, } p=Pool() res_l=[] for url,pattern in url_dic.items(): res=p.apply_async(get_page,args=(url,pattern),callback=parse_page) res_l.append(res) for i in res_l: i.get() # res=requests.get('http://maoyan.com/board/7') # print(re.findall(pattern,res.text)) **如果在主进程中等待进程池中所有任务都执行完毕后,再统一处理结果,则无需回调函数** from multiprocessing import Pool import time,random,os def work(n): time.sleep(1) return n**2 if __name__ == '__main__': p=Pool() res_l=[] for i in range(10): res=p.apply_async(work,args=(i,)) res_l.append(res) p.close() p.join() #等待进程池中所有进程执行完毕 nums=[] for res in res_l: nums.append(res.get()) #拿到所有结果 print(nums) #主进程拿到所有的处理结果,可以在主进程中进行统一进行处理 [**进程池的其他实现方式:https://docs.python.org/dev/library/concurrent.futures.html**][https_docs.python.org_dev_library_concurrent.futures.html] 转载于:https://www.cnblogs.com/wanghaohao/p/7444827.html [https_docs.python.org_dev_library_concurrent.futures.html]: https://docs.python.org/dev/library/concurrent.futures.html
相关 回调函数 一、回调函数 我们经常在C++设计时通过使用回调函数可以使有些应用(如定时器事件回调处理、用回调函数记录某操作进度等)变得非常方便和符合逻辑,那么它的内在机制如何呢,怎么定义 ╰+攻爆jí腚メ/ 2022年06月16日 23:37/ 0 赞/ 215 阅读
相关 回调函数 回调函数是一种函数指针的使用方式。 stdcall和cdecl的区别:这个问题涉及到函数调用的汇编知识,暂只需记住stdcall和cdecl的参数压栈顺序是相同的,参 傷城~/ 2022年06月12日 07:21/ 0 赞/ 219 阅读
相关 回调函数 转自: 回调函数 在看LWIP时,见到用回调函数,再看某老外公司OPC源代码时,见到用回调函数。看我国内某些代码( 朴灿烈づ我的快乐病毒、/ 2022年06月11日 05:36/ 0 赞/ 188 阅读
相关 回调函数 回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用它所指向的函数时,我们就说这是回调函数。回调函数不是由该函数的实 青旅半醒/ 2022年06月02日 20:51/ 0 赞/ 230 阅读
相关 回调函数 实际工作中,对于回调函数一直是我不愿意去触碰的东西,一来由于被很多人搞得神秘兮兮的以为是很高深难懂的技术,二来在一般情况下通过互相包含类指针也能够解决问题,所以一直就不想去研究 迈不过友情╰/ 2022年04月15日 06:20/ 0 赞/ 267 阅读
相关 回调函数 需要回调函数的场景:进程池中任何一个任务一旦处理完了,就立即告知主进程:我好了额,你可以处理我的结果了。主进程则调用一个函数去处理该结果,该函数即回调函数 我们可以把耗时间( 女爷i/ 2022年01月07日 07:41/ 0 赞/ 304 阅读
相关 回调函数 转载自[https://zhidao.baidu.com/question/572699725.html][https_zhidao.baidu.com_question_57 水深无声/ 2021年12月12日 04:01/ 0 赞/ 375 阅读
相关 回调函数 作为JS的核心,回调函数和异步执行是紧密相关的,也是必须跨过去的一道个门槛。 那么究竟什么是回调函数(Callback),其实回调函数并不复杂,明白两个重点即可: 1. 函 桃扇骨/ 2021年11月13日 21:28/ 0 赞/ 430 阅读
相关 回调函数 一、定义 什么是回调函数?它首先是一个函数,这是肯定的,我举个例子:有两个函数A和B,首先应用程序将A的地址告诉B,这称之为注册回调函数,B在收到外部某个命令的时候,可以根据 客官°小女子只卖身不卖艺/ 2021年09月30日 11:56/ 0 赞/ 476 阅读
相关 回调与回调函数 回调与回调函数 回调 回调函数 回调 回调指的是某函数在执行的时候,由于某些原因,该函数被迫中止执行,交出当前线程的使用权,当前线程转而执行其它函 悠悠/ 2021年09月25日 04:26/ 0 赞/ 474 阅读
还没有评论,来说两句吧...