首页 > 解决方案 > Python 池多处理请求挂起

问题描述

我的程序中的代码基本上同时执行 10 个 python 请求并同时处理它们的输出,它工作了一段时间,但我改变了一些东西,但无法弄清楚是什么破坏了它。

以下代码是调用的代码,代码似乎在第 3 行和第 4 行之间冻结,因此在执行多线程请求的过程中。

'print("failed to close") 行没有打印出来,似乎表明程序没有到达 pool.close() 指令。

listoftensites = listoftensites
pool = Pool(processes=10) # Initalize a pool of 10 processes
listoftextis, listofonline = zip(*pool.map(onionrequestthreaded, listoftensites)) # Use the pool to run the function on the items in the iterable
print("failed to close ")
pool.close()
 # this means that no more tasks will be added to the pool
pool.join() 

它挂起时调用的函数紧跟在'print(“failed in return”)'行之后,这似乎表明请求没有正确终止并返回预期值。

def onionrequestthreaded(onionurl):
    session = requests.session()
    session.proxies = {}
    session.proxies['http'] = 'socks5h://localhost:9050'
    session.proxies['https'] = 'socks5h://localhost:9050'

    onionurlforrequest = "http://" + onionurl
    #print(onionurlforrequest)
    print("failed with proxy session")
    try:
        print("failed in request")
        r = session.get(onionurlforrequest, timeout=15, allow_redirects=True)
        online = 2
        print("failed in text extraction")

        textis = r.text
    except:
        print("failed in except")

        #print("failed")
        online = 1

        textis = ""
    print("failed in return")
    return textis, online

非常令人困惑,但我可能正在做一些简单的事情。请让我知道是否有解决方案,因为我正在拔头发。

标签: pythonpython-3.xpython-requestspython-multiprocessing

解决方案


推荐阅读