簡體   English   中英

為什么我的 multiprocessing.Pool apply_async 只在 for 循環中執行一次

[英]Why is my multiprocessing.Pool apply_async only executed once inside a for loop

我正在嘗試為 web 安全項目編寫爬蟲,並且使用多處理的方法出現了奇怪的行為。

這個方法應該怎么做? 它使用找到的查詢參數列表遍歷找到的目標 web 頁面。 對於每個 web 頁面,它應該將方法phase1 (我的攻擊邏輯)應用於與該頁面關聯的每個查詢參數。

Meaning, if I have http://example.com/sub.php , having page &secret as query parameters, and http://example.com/s2.php , having topsecret as parameter, it should do the following:

我知道是否發生了攻擊,基於時間和 phase1 的output

實際發生了什么

只執行第一次攻擊。 以下對 apply_async 的調用將被忽略。 但是,它仍然在循環中循環,因為它仍然從上面的 for 循環打印 output。

這里出了什么問題? 為什么沒有觸發攻擊例程? 我查看了多處理的文檔,但這無助於解釋這種現象。

相關問題中的一些答案建議使用終止和加入,但是這不是在這里隱式完成的,因為我使用的是 with 語句?

另外,這個問題( 多處理池'apply_async'似乎只調用一次function )聽起來很相似,但與我的問題不同。 與那個問題相反,我不存在只有 1 個工作人員執行代碼的問題,但我的 X 個工作人員只產生了一次(而不是 Y 次)。

我嘗試過的:將 with..Pool 放在循環之外,但沒有任何改變

有問題的方法如下:

def analyzeParam(siteparams, paysplit, victim2, verbose, depth, file, authcookie):
    result = {}
    subdir = parseUrl(viclist[0])
    for victim, paramlist in siteparams.items():
        sub = {}
        print("\n{0}[INFO]{1} param{4}|{2} Attacking {3}".format(color.RD, color.END + color.O, color.END, victim, color.END+color.RD))
        time.sleep(1.5)
        for param in paramlist:
            payloads = []
            nullbytes = []
            print("\n{0}[INFO]{1} param{4}|{2} Using {3}\n".format(color.RD, color.END + color.O, color.END, param, color.END+color.RD))
            time.sleep(1.5)
            with Pool(processes=processes) as pool:
                res = [pool.apply_async(phase1, args=(1,victim,victim2,param,None,"",verbose,depth,l,file,authcookie,"",)) for l in paysplit]
                for i in res:
                    #fetch results
                    tuples = i.get()
                    payloads += tuples[0]
                    nullbytes += tuples[1]
            sub[param] = (payloads, nullbytes)
            time.sleep(3)
        result[victim] = sub
    if not os.path.exists(cachedir+subdir):
        os.makedirs(cachedir+subdir)
    with open(cachedir+subdir+"spider-phase2.json", "w+") as f:
        json.dump(result, f, sort_keys=True, indent=4)
    return result

一些技術資料:

  • Python 版本:3.8.5
  • 我懷疑該錯誤存在於phase1 ,因為當在循環外使用 Pool 調用時,但多次調用時,它會按預期運行。 想查的話,源碼在這里: https://github.com/VainlyStrain/Vailyn

我該如何解決? 謝謝!

非常感謝 jasonharper 發現問題,問題不是上面的代碼結構,而是變量 paysplit。 這是一台發電機,在第一次通話后就筋疲力盡了。

再次感謝您的指出!

最好的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM