pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <i id='yE6jm'><tr id='yE6jm'><dt id='yE6jm'><q id='yE6jm'><span id='yE6jm'><b id='yE6jm'><form id='yE6jm'><ins id='yE6jm'></ins><ul id='yE6jm'></ul><sub id='yE6jm'></sub></form><legend id='yE6jm'></legend><bdo id='yE6jm'><pre id='yE6jm'><center id='yE6jm'></center></pre></bdo></b><th id='yE6jm'></th></span></q></dt></tr></i><div class="2xzkjpy" id='yE6jm'><tfoot id='yE6jm'></tfoot><dl id='yE6jm'><fieldset id='yE6jm'></fieldset></dl></div>
      <tfoot id='yE6jm'></tfoot>
      <legend id='yE6jm'><style id='yE6jm'><dir id='yE6jm'><q id='yE6jm'></q></dir></style></legend>

      • <bdo id='yE6jm'></bdo><ul id='yE6jm'></ul>

      1. <small id='yE6jm'></small><noframes id='yE6jm'>

      2. 如何使用多處理循環遍歷一大串 URL?

        How to use multiprocessing to loop through a big list of URL?(如何使用多處理循環遍歷一大串 URL?)
        <tfoot id='7w6Y1'></tfoot>
            <bdo id='7w6Y1'></bdo><ul id='7w6Y1'></ul>
            • <legend id='7w6Y1'><style id='7w6Y1'><dir id='7w6Y1'><q id='7w6Y1'></q></dir></style></legend>

                <i id='7w6Y1'><tr id='7w6Y1'><dt id='7w6Y1'><q id='7w6Y1'><span id='7w6Y1'><b id='7w6Y1'><form id='7w6Y1'><ins id='7w6Y1'></ins><ul id='7w6Y1'></ul><sub id='7w6Y1'></sub></form><legend id='7w6Y1'></legend><bdo id='7w6Y1'><pre id='7w6Y1'><center id='7w6Y1'></center></pre></bdo></b><th id='7w6Y1'></th></span></q></dt></tr></i><div class="82ko8h3" id='7w6Y1'><tfoot id='7w6Y1'></tfoot><dl id='7w6Y1'><fieldset id='7w6Y1'></fieldset></dl></div>
              1. <small id='7w6Y1'></small><noframes id='7w6Y1'>

                  <tbody id='7w6Y1'></tbody>

                  本文介紹了如何使用多處理循環遍歷一大串 URL?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  問題:檢查超過 1000 個 url 的列表并獲取 url 返回碼(status_code).

                  我的腳本可以運行,但速度很慢.

                  我認為必須有一種更好的、pythonic(更漂亮)的方式來執行此操作,我可以在其中生成 10 或 20 個線程來檢查 url 并收集 resonses.(即:

                  200 ->www.yahoo.com404->www.badurl.com...


                  輸入文件:Url10.txt

                  www.example.comwww.yahoo.comwww.testsite.com

                  ....

                  導入請求使用 open("url10.txt") 作為 f:urls = f.read().splitlines()打印(網址)對于網址中的網址:url = 'http://'+url #將http://添加到每個url(必須有更好的方法來做到這一點)嘗試:resp = requests.get(url, timeout=1)print(len(resp.content), '->', resp.status_code, '->', resp.url)例外為 e:打印(錯誤",網址)

                  挑戰:通過多處理提高速度.


                  多處理

                  但它不工作.我收到以下錯誤:(注意:我不確定我是否正確地實現了這個)

                  AttributeError: Can't get attribute 'checkurl' on <module '__main__' (built-in)>

                  --

                  導入請求從多處理導入池使用 open("url10.txt") 作為 f:urls = f.read().splitlines()def checkurlconnection(url):對于網址中的網址:url = 'http://'+url嘗試:resp = requests.get(url, timeout=1)print(len(resp.content), '->', resp.status_code, '->', resp.url)例外為 e:打印(錯誤",網址)如果 __name__ == __main__":p = 池(進程=4)結果 = p.map(checkurlconnection, urls)

                  解決方案

                  在這種情況下,您的任務受 I/O 限制而非處理器限制 - 網站回復所需的時間比 CPU 循環一次所需的時間長您的腳本(不包括 TCP 請求).這意味著您不會從并行執行此任務中獲得任何加速(這就是 multiprocessing 所做的).你想要的是多線程.實現這一點的方法是使用文檔很少,可能名稱不佳的 multiprocessing.dummy:

                  導入請求from multiprocessing.dummy import Pool as ThreadPoolurls = ['https://www.python.org','https://www.python.org/about/']def get_status(url):r = requests.get(url)返回 r.status_code如果 __name__ == "__main__":pool = ThreadPool(4) # 建立工人池results = pool.map(get_status, urls) #在自己的線程中打開urlpool.close() #關閉池并等待工作完成pool.join()

                  參見此處,了解 Python 中多處理與多線程的示例.p>

                  Problem: Check a listing of over 1000 urls and get the url return code (status_code).

                  The script I have works but very slow.

                  I am thinking there has to be a better, pythonic (more beutifull) way of doing this, where I can spawn 10 or 20 threads to check the urls and collect resonses. (i.e:

                  200 -> www.yahoo.com
                  404 -> www.badurl.com
                  ...
                  


                  Input file:Url10.txt

                  www.example.com
                  www.yahoo.com
                  www.testsite.com
                  

                  ....

                  import requests
                  
                  with open("url10.txt") as f:
                      urls = f.read().splitlines()
                  
                  print(urls)
                  for url in urls:
                      url =  'http://'+url   #Add http:// to each url (there has to be a better way to do this)
                      try:
                          resp = requests.get(url, timeout=1)
                          print(len(resp.content), '->', resp.status_code, '->', resp.url)
                      except Exception as e:
                          print("Error", url)
                  

                  Challenges: Improve speed with multiprocessing.


                  With multiprocessing

                  But is it not working. I get the following error: (note: I am not sure if I have even implemented this correctly)

                  AttributeError: Can't get attribute 'checkurl' on <module '__main__' (built-in)>
                  

                  --

                  import requests
                  from multiprocessing import Pool
                  
                  with open("url10.txt") as f:
                      urls = f.read().splitlines()
                   
                  def checkurlconnection(url):
                      
                      for url in urls:
                          url =  'http://'+url
                          try:
                              resp = requests.get(url, timeout=1)
                              print(len(resp.content), '->', resp.status_code, '->', resp.url)
                          except Exception as e:
                              print("Error", url)
                          
                  if __name__ == "__main__":
                      p = Pool(processes=4)
                      result = p.map(checkurlconnection, urls)
                  

                  解決方案

                  In this case your task is I/O bound and not processor bound - it takes longer for a website to reply than it does for your CPU to loop once through your script (not including the TCP request). What this means is that you wont get any speedup from doing this task in parallel (which is what multiprocessing does). What you want is multi-threading. The way this is achieved is by using the little documented, perhaps poorly named, multiprocessing.dummy:

                  import requests
                  from multiprocessing.dummy import Pool as ThreadPool 
                  
                  urls = ['https://www.python.org',
                          'https://www.python.org/about/']
                  
                  def get_status(url):
                      r = requests.get(url)
                      return r.status_code
                  
                  if __name__ == "__main__":
                      pool = ThreadPool(4)  # Make the Pool of workers
                      results = pool.map(get_status, urls) #Open the urls in their own threads
                      pool.close() #close the pool and wait for the work to finish 
                      pool.join() 
                  

                  See here for examples of multiprocessing vs multithreading in Python.

                  這篇關于如何使用多處理循環遍歷一大串 URL?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多處理模塊的 .join() 方法到底在做什么?)
                  Passing multiple parameters to pool.map() function in Python(在 Python 中將多個參數傳遞給 pool.map() 函數)
                  multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEncodingError: TypeError(cannot serialize _io.BufferedReader object,)) - IT屋-程序員軟件開
                  Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多進程池.當其中一個工作進程確定不再需要完成工作時,如何退出腳本?) - IT屋-程序員
                  How do you pass a Queue reference to a function managed by pool.map_async()?(如何將隊列引用傳遞給 pool.map_async() 管理的函數?)
                  yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(與多處理錯誤的另一個混淆,“模塊對象沒有屬性“f)
                  • <bdo id='lV24I'></bdo><ul id='lV24I'></ul>

                    <small id='lV24I'></small><noframes id='lV24I'>

                    • <tfoot id='lV24I'></tfoot>
                        <tbody id='lV24I'></tbody>
                    • <i id='lV24I'><tr id='lV24I'><dt id='lV24I'><q id='lV24I'><span id='lV24I'><b id='lV24I'><form id='lV24I'><ins id='lV24I'></ins><ul id='lV24I'></ul><sub id='lV24I'></sub></form><legend id='lV24I'></legend><bdo id='lV24I'><pre id='lV24I'><center id='lV24I'></center></pre></bdo></b><th id='lV24I'></th></span></q></dt></tr></i><div class="3k8ihye" id='lV24I'><tfoot id='lV24I'></tfoot><dl id='lV24I'><fieldset id='lV24I'></fieldset></dl></div>
                      1. <legend id='lV24I'><style id='lV24I'><dir id='lV24I'><q id='lV24I'></q></dir></style></legend>

                          • 主站蜘蛛池模板: R507制冷剂,R22/R152a制冷剂厂家-浙江瀚凯制冷科技有限公司 | 汽车水泵_汽车水泵厂家-瑞安市骏迪汽车配件有限公司 | 选矿设备-新型重选设备-金属矿尾矿重选-青州冠诚重工机械有限公司 | 冷藏车厂家|冷藏车价格|小型冷藏车|散装饲料车厂家|程力专用汽车股份有限公司销售十二分公司 | 线粒体膜电位荧光探针-细胞膜-标记二抗-上海复申生物科技有限公司 | 压缩空气检测_气体_水质找上海京工-服务专业、价格合理 | 上海噪音治理公司-专业隔音降噪公司-中广通环保 | 广州活动策划公司-15+年专业大型公关活动策划执行管理经验-睿阳广告 | 华禹护栏|锌钢护栏_阳台护栏_护栏厂家-华禹专注阳台护栏、楼梯栏杆、百叶窗、空调架、基坑护栏、道路护栏等锌钢护栏产品的生产销售。 | 地脚螺栓_材质_标准-永年县德联地脚螺栓厂家 | 挨踢网-大家的导航!| 衬氟止回阀_衬氟闸阀_衬氟三通球阀_衬四氟阀门_衬氟阀门厂-浙江利尔多阀门有限公司 | 建筑资质代办_工程施工资质办理_资质代办公司_北京众聚企服 | 隆众资讯-首页_大宗商品资讯_价格走势_市场行情 | 科普仪器菏泽市教育教学仪器总厂| 高中学习网-高考生信息学习必备平台 | 皮带机_移动皮带机_大倾角皮带机_皮带机厂家 - 新乡市国盛机械设备有限公司 | 合肥展厅设计-安徽展台设计-合肥展览公司-安徽奥美展览工程有限公司 | 铆钉机|旋铆机|东莞旋铆机厂家|鸿佰专业生产气压/油压/自动铆钉机 | 天一线缆邯郸有限公司_煤矿用电缆厂家_矿用光缆厂家_矿用控制电缆_矿用通信电缆-天一线缆邯郸有限公司 | 洛阳装修公司-洛阳整装一站式品牌-福尚云宅装饰 | 青岛空压机,青岛空压机维修/保养,青岛空压机销售/出租公司,青岛空压机厂家电话 | 深圳3D打印服务-3D打印加工-手板模型加工厂-悟空打印坊 | 透平油真空滤油机-变压器油板框滤油机-滤油车-华之源过滤设备 | 精密五金冲压件_深圳五金冲压厂_钣金加工厂_五金模具加工-诚瑞丰科技股份有限公司 | 济南品牌包装设计公司_济南VI标志设计公司_山东锐尚文化传播 | pos机办理,智能/扫码/二维码/微信支付宝pos机-北京万汇通宝商贸有限公司 | 江苏皓越真空设备有限公司 | 哈尔滨治「失眠/抑郁/焦虑症/精神心理」专科医院排行榜-京科脑康免费咨询 一对一诊疗 | 高通量组织研磨仪-多样品组织研磨仪-全自动组织研磨仪-研磨者科技(广州)有限公司 | 高低温老化试验机-步入式/低温恒温恒湿试验机-百科 | 涿州网站建设_网站设计_网站制作_做网站_固安良言多米网络公司 | 基本型顶空进样器-全自动热脱附解吸仪价格-AutoHS全模式-成都科林分析技术有限公司 | 无线联网门锁|校园联网门锁|学校智能门锁|公租房智能门锁|保障房管理系统-KEENZY中科易安 | 宁波普瑞思邻苯二甲酸盐检测仪,ROHS2.0检测设备,ROHS2.0测试仪厂家 | 刚性-柔性防水套管-橡胶伸缩接头-波纹管补偿器-启腾供水材料有限公司 | 橡胶膜片,夹布膜片,橡胶隔膜密封,泵阀设备密封膜片-衡水汉丰橡塑科技公司网站 | 播音主持培训-中影人教育播音主持学苑「官网」-中国艺考界的贵族学校 | 电动手术床,医用护理床,led手术无影灯-曲阜明辉医疗设备有限公司 | 南溪在线-南溪招聘找工作、找房子、找对象,南溪综合生活信息门户! | 南溪在线-南溪招聘找工作、找房子、找对象,南溪综合生活信息门户! |