이 모든 것은 Python 버전 3.x에 있습니다.
200을 확인하는 작업자 스레드를 만들 것입니다. 예제를 제공합니다. (threadpool.py에 넣어)이 스레드는이 프로그램에 따라 확장하는 것이
가
numconns = 40
workers = threadpool.ThreadPool(numconns)
results = [None] * len(urllist)
def check200(url, index):
results[index] = is_200(url)
for index, url in enumerate(urllist):
try:
workers.add_task(check200, url, index)
except KeyboardInterrupt:
print("Shutting down application, hang on...")
workers.wait_completion()
break
참고 : urllist
이 URL을 포함하는 경우
# http://code.activestate.com/recipes/577187-python-thread-pool/
from queue import Queue
from threading import Thread
class Worker(Thread):
def __init__(self, tasks):
Thread.__init__(self)
self.tasks = tasks
self.daemon = True
self.start()
def run(self):
while True:
func, args, kargs = self.tasks.get()
try: func(*args, **kargs)
except Exception as exception: print(exception)
self.tasks.task_done()
class ThreadPool:
def __init__(self, num_threads):
self.tasks = Queue(num_threads)
for _ in range(num_threads): Worker(self.tasks)
def add_task(self, func, *args, **kargs):
self.tasks.put((func, args, kargs))
def wait_completion(self):
self.tasks.join()
이제 다음 주 파일이의 라인을 따라해야 여기에 게시 된 다른 제안 사항은 is_200()
에만 의존합니다.
비동기 입출력을 처리 할 때 node.js가 도움이 될 수도 있습니다. 또한 가능한 경우이를 위해 컴퓨터 클러스터를 사용할 수 있습니다. – Thilo