1

다음과 같은 문제가 있습니다. 지금까지는 웹 크롤러를 구현하고 싶었지만 지금까지는 작동했지만 너무 느려서 URL을 가져 오는 데 멀티 프로세싱을 사용하려고했습니다. 불행히도 저는이 분야에서 경험이별로 없습니다. 몇 가지 독서 후 가장 쉬운 방법은 내게 map 방법을 사용하는 것 같았습니다. multiprocessing.poolPython Multiprocessing - 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 TestCrawler 밖에서 Queue 인스턴스를 이동

있습니다.
이것은 또 다른 오류에 이르게 :

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