1

次の問題があります。これまでのところ、Webクローラーを実装したいと思っていましたが、遅かったので、URLを取得するためにマルチプロセッシングを使用しようとしました。 残念ながら私はこの分野ではあまり経験がありません。 読んだ後、最も簡単な方法は、mapメソッドをmultiprocessing.poolから使用するように思えました。Pythonマルチプロセッシング - TypeError:セキュリティ上の理由から、AuthenticationStringオブジェクトのpicklingが許可されていません

しかし、私は常に次のエラーを取得する:

TypeError: Pickling an AuthenticationString object is disallowed for security reasons 

私は同じエラーで非常に少数の例を発見し、彼らが、残念ながら私を助けていませんでした。

import multiprocessing 

class TestCrawler: 
    def __init__(self): 
     self.m = multiprocessing.Manager() 
     self.queue = self.m.Queue() 
     for i in range(50): 
      self.queue.put(str(i)) 
     self.pool = multiprocessing.Pool(6) 



    def mainloop(self): 
     self.process_next_url(self.queue) 

     while True: 
      self.pool.map(self.process_next_url, (self.queue,))     

    def process_next_url(self, queue): 
     url = queue.get() 
     print(url) 


c = TestCrawler() 
c.mainloop() 

私は任意のヘルプや提案について非常に感謝し、次のようになります。

私は、エラーを再現することができます私のコードの被覆のバージョンを作成しました!

答えて

2

Question: But I constantly get the following error:

you'rがmissleadingさ取得エラー、理由は

self.queue = self.m.Queue() 
class TestCrawlerQueueのインスタンスを移動し

あります。
これは、別のエラーにつながる:

NotImplementedError: pool objects cannot be passed between processes or pickled

理由は以下のとおりです。

self.pool = multiprocessing.Pool(6) 

どちらのエラーがpickleclass Membersを見つけることができないことを示しています。

class TestCrawler2: 
    def __init__(self, tasks): 
     self.tasks = tasks 

    def start(self): 
     pool = mp.Pool(5) 
     pool.map(self.process_url, self.tasks) 

    def process_url(self, url): 
     print('self.process_url({})'.format(url)) 

if __name__ == "__main__": 
    tasks = ['url{}'.format(n) for n in range(50)] 
    TestCrawler2(tasks).start() 

class TestCrawler: 
    def __init__(self, tasks): 
     # Assign the Global task to class member 
     self.queue = tasks 
     for i in range(50): 
      self.queue.put(str(i)) 

    def mainloop(self): 
     # Instantiate the pool local 
     pool = mp.Pool(6) 
     for n in range(50): 
      # .map requires a Parameter pass None 
      pool.map(self.process_next_url, (None,)) 

    # None is passed 
    def process_next_url(self, dummy): 
     url = self.queue.get() 
     print(url) 

if __name__ == "__main__": 
    # Create the Queue as Global 
    tasks = mp.Manager().Queue() 
    # Pass the Queue to your class TestCrawler 
    c = TestCrawler(tasks) 
    c.mainloop() 

この例では5つのプロセスの各処理10個のタスク(URL)を開始します:

Note: Endless Loop!
Your following while Loop leads to a Endless Loop! This will overload your System!
Furthermore, your pool.map(... starts only oneProcess with one Task!

while True: 
     self.pool.map(self.process_next_url, (self.queue,)) 

は、私は次のようにThe Examples that demonstrates the use of a pool


変化を読み取​​ることをお勧めパイソンでテスト

:3.4.2

+0

は、あなたの答えをありがとうございました!簡単な例ではうまくいきましたが、私のプログラムで変更したときに、何とか同じような問題が残っているように見えます。多分、酸洗を説明する資源を私に送ってもらえますか?これを探しているときに、私は主にpickleモジュールに関するテキストを取得します。 –

+0

@blue:あなたの質問を編集し、** args = **あなたが 'pool.map(...')のために使用していることを表示してください、あなたはまだ 'muliprocessing'オブジェクトを渡すことはできません! – stovfl

関連する問題