2017-01-13 5 views
0

multithreadingライブラリを使用してスクリプトをmultiprocessingライブラリに移植したため、プロセス間でメモリが共有される方法に関する問題が発生しています。マルチプロセッシングループおよびストップコンディション

クイック概要、私の労働者は単語の列を消費しています。作業者がヒットした場合、他の実行中のプロセスを終了させるためにシグナル(グローバル変数または実装)をホールドキャストする必要があります。ここで

は私の労働者の主な方法である:ここでは

def run(self): 
    while not self.queue.empty(): 
     entry = self.queue.get() 

     try: 
      payload = jwt.decode(self.token, entry, algorithm = 'HS256') 
     except jwt.InvalidTokenError: 
      if self.verbose: 
       print(DEBUG + "[{}] ".format(self.name) + "InvalidTokenError: " + Style.BRIGHT + entry + RESET) 
      continue 
     except jwt.DecodeError: 
      print(WARNING + "[{}] ".format(self.name) + "DecodingError: " + Style.BRIGHT + entry + RESET) 
      continue 
     except Exception as ex: 
      print(ERROR + "[{}] ".format(self.name) + "Exception: " + Style.BRIGHT + "{}".format(ex) + RESET) 
      continue 

     # Save the holy secret into a file in case sys.stdout is not responding 
     with open("jwtpot.pot", "a+") as file: 
      file.write("{0}:{1}:{2}".format(self.token, payload, entry)) 
      print(RESULT + "[{}] ".format(self.name) + "Secret key saved to location: " + Style.BRIGHT + "{}".format(file.name) + RESET) 

     print(RESULT + "[{}] ".format(self.name) + "Secret key: " + Style.BRIGHT + entry + RESET) 
     print(RESULT + "[{}] ".format(self.name) + "Payload: " + Style.BRIGHT + "{}".format(payload) + RESET) 

     break 

     self.queue.task_done() 

は私がインスタンス化し、私のメインの私のプロセスを起動する方法です:

# Load and segmentate the wordlist into the queue 
      print(INFO + "Processing the wordlist..." + RESET) 
      queue = populate_queue(queue, wordlist, verbose) 

      print(INFO + "Total retrieved words: " + Style.BRIGHT + "{}".format(queue.qsize()) + RESET) 

      for i in range(process_count): 
       process = Process(queue, token, verbose) 
       process.daemon = True 
       print(INFO + "Starting {}".format(process.name) + RESET) 
       process.start() 
       processes.append(process) 

      print(WARNING + "Pour yourself some coffee, this might take a while..." + RESET) 

      # Block the parent-process until all the child-processes finish to process the queue 
      for process in processes: 
       process.join() 

答えて

0

私はすべての(共有)のパイプを作成しますサブプロセスは親プロセスに戻る。次に、秘密鍵を検出するプロセスは、何が見つかったかを示すためにパイプに何かを書き込むことができます。プロセスが秘密鍵を見つけず、キューが空の場合、プロセスはただ終了します。

親は、パイプから何かを取得するまで単純に待機します。これは、子がパイプに書き込むとき、またはすべての子が終了したときに発生します。その後、まだ実行中の残りの子をすべて削除します。

ここでは簡単ハックの実証です:

from multiprocessing import * 
from time import sleep 

def process(pid, rpipe, wpipe): 
    rpipe.close() 
    sleep(1 + pid * 0.1) 
    if pid == 5: 
     print("I found it!") 
     wpipe.send((pid, "GOT IT")) 
    print("Process %d exiting" % pid) 
    wpipe.close() 

def one_try(findit): 
    processes = [] 
    rpipe, wpipe = Pipe() 

    for i in range(15): 
     # Start 
     if i != 5 or findit: 
      p = Process(target=process, args=(i, rpipe, wpipe)) 
      p.start() 
      processes.append((i, p)) 

    # Close write pipe in the parent so we get EOF when all children are gone 
    wpipe.close() 
    try: 
     pid, result = rpipe.recv() 
     print("%s was found by %s" % (result, pid)) 
     print("Will kill other processes") 
    except EOFError: 
     print("Nobody found it!") 
    rpipe.close() 

    for i, p in processes: 
     p.terminate() 
     p.join() 

one_try(True)  # Should have one process that finds it 
one_try(False)  # Nobody found it