2010-06-26 5 views
1

저는 도메인 목록을 읽고, Mcafee의 Siteadvisor 서비스가 제공하는 등급을 찾아서 도메인과 결과를 CSV로 출력하는 파이썬 스크립트를 작성하고 있습니다.이 다중 스레드 Python 스크립트를 어떻게 수정합니까?

나는 내 스크립트를 this previous answer에서 삭제했습니다. 그것은 urllib을 사용하여 문제의 도메인에 대한 Siteadvisor의 페이지를 긁습니다 (최선의 방법은 아니지만 Siteadvisor는 대안을 제공하지 않습니다). 불행하게도, 실패 아무것도 생산하는 - 내가 지속적으로이 오류 : 어떤 도움을 크게 감상 할 수

import threading 
import urllib 

class Resolver(threading.Thread): 
    def __init__(self, address, result_dict): 
     threading.Thread.__init__(self) 
     self.address = address 
     self.result_dict = result_dict 

    def run(self): 
     try: 
      content = urllib.urlopen("http://www.siteadvisor.com/sites/" + self.address).read(12000) 
      search1 = content.find("didn't find any significant problems.") 
      search2 = content.find('yellow') 
      search3 = content.find('web reputation analysis found potential security') 
      search4 = content.find("don't have the results yet.") 

      if search1 != -1: 
       result = "safe" 
      elif search2 != -1: 
       result = "caution" 
      elif search3 != -1: 
       result = "warning" 
      elif search4 != -1: 
       result = "unknown" 
      else: 
       result = "" 

      self.result_dict[self.address] = result 

     except: 
      pass 


def main(): 
    infile = open("domainslist", "r") 
    intext = infile.readlines() 
    threads = [] 
    results = {} 
    for address in [address.strip() for address in intext if address.strip()]: 
     resolver_thread = Resolver(address, results) 
     threads.append(resolver_thread) 
     resolver_thread.start() 

    for thread in threads: 
     thread.join() 

    outfile = open('final.csv', 'w') 
    outfile.write("\n".join("%s,%s" % (address, ip) for address, ip in results.iteritems())) 
    outfile.close() 

if __name__ == '__main__': 
    main() 

:

Traceback (most recent call last): 
    File "multi.py", line 55, in <module> 
    main() 
    File "multi.py", line 44, in main 
    resolver_thread.start() 
    File "/usr/lib/python2.6/threading.py", line 474, in start 
    _start_new_thread(self.__bootstrap,()) 
thread.error: can't start new thread 

여기 내 스크립트입니다.

+0

디버깅 해보십시오. –

+1

몇 개의 스레드를 만들고 있습니까? –

답변

1

너무 많은 스레드를 시작하려고합니다.

[address.strip() for address in intext if address.strip()] 목록에있는 항목 수를 확인할 수 있습니다. 나는 이것이 여기서 문제가 될 수 있다고 생각한다. 기본적으로 새 스레드를 시작할 수있는 사용 가능한 리소스의 한계가 있습니다.

20 개의 요소를 말하고, 20 개의 스레드로 작업을하고, 스레드가 작업을 마칠 때까지 기다린 다음 다음 덩어리를 선택하는 방법으로 목록을 청크하는 것입니다. 목록의 모든 요소가 처리 될 때까지이 작업을 수행하십시오.

더 나은 스레드 관리를 위해 일부 스레드 풀을 사용할 수도 있습니다. (최근에 this implementation을 사용했습니다).

+0

좋은 생각 같네요. 감사합니다 – Tom

+0

다행 당신을 도울 수 있어요. – dzida

1

만들 수있는 스레드 수의 상한선 일 가능성이 높습니다. 아마 초과 할 것입니다.

제안 : 작고 고정 된 수의 리졸버를 만듭니다. 10 미만이면 가능한 병렬 처리 이점의 90 %를 얻게 될 것이며, 파이썬 대기열 lib의 (threadsafe) Queue를 얻을 수 있습니다. 주 스레드가 모든 도메인을 큐에 덤프시키고 각 Resolver가 큐에서 한 번에 하나의 도메인을 가져 와서 작업하도록하십시오.

관련 문제