久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <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="qwawimqqmiuu" 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. 如何使用多處理循環(huán)遍歷一大串 URL?

        How to use multiprocessing to loop through a big list of URL?(如何使用多處理循環(huán)遍歷一大串 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="qwawimqqmiuu" 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>

                  本文介紹了如何使用多處理循環(huán)遍歷一大串 URL?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  限時(shí)送ChatGPT賬號(hào)..

                  問(wèn)題:檢查超過(guò) 1000 個(gè) url 的列表并獲取 url 返回碼(status_code).

                  我的腳本可以運(yùn)行,但速度很慢.

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

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


                  輸入文件:Url10.txt

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

                  ....

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

                  挑戰(zhàn):通過(guò)多處理提高速度.


                  多處理

                  但它不工作.我收到以下錯(cuò)誤:(注意:我不確定我是否正確地實(shí)現(xiàn)了這個(gè))

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

                  --

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

                  解決方案

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

                  導(dǎo)入請(qǐng)求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) #在自己的線(xiàn)程中打開(kāi)urlpool.close() #關(guān)閉池并等待工作完成pool.join()

                  參見(jiàn)此處,了解 Python 中多處理與多線(xiàn)程的示例.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.

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

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

                  相關(guān)文檔推薦

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

                          • 主站蜘蛛池模板: 精品区| 一区二区三区四区国产 | 日本午夜精品一区二区三区 | 久久久.com| a级大片免费观看 | 999久久久久久久久6666 | 欧美日韩a| 精品国产一区二区久久 | 国产羞羞视频在线观看 | 91精品久久久久久久久久 | 91日b| 久久精品一区二区三区四区 | 97人人澡人人爽91综合色 | 中文字幕的av| 97天天干 | 91精品国产欧美一区二区 | 欧洲一区二区视频 | 国产91 在线播放 | 黄色网址免费在线观看 | 欧美在线精品一区 | 欧美伊人| 在线观看 亚洲 | 成人免费视频在线观看 | 黄色大片免费看 | 91精品国产91久久久久久最新 | 欧美一级二级在线观看 | 国产精品久久久久久久午夜片 | a级毛片基地 | 中文在线一区二区 | 九九99久久 | 成人精品国产免费网站 | 亚洲精品日韩精品 | 久久草视频 | 国产一区二区三区高清 | 免费黄色在线观看 | 日韩一二区 | 欧美日韩成人在线 | 中文字幕日本一区二区 | 欧美激情综合五月色丁香小说 | 久久精品久久久久久 | 国产精品大片在线观看 |