该代码适用于单个“管理器”,它基本上将一些 HTTP GET 发送到服务器。但我碰上了一堵砖墙。
我现在如何创建 2 个管理器,每个管理器都有自己的 Download_Dashlet_Job 对象和 tcp_pool_object?从本质上讲,经理们将指挥他们自己的工人从事两个不同的工作。这似乎是学习 Python 类的一个非常好的难题。
import workerpool
from urllib3 import HTTPConnectionPool
class Download_Dashlet_Job(workerpool.Job):
def __init__(self, url):
self.url = url
def run(self):
request = tcp_pool_object.request('GET', self.url, headers=headers)
tcp_pool_object = HTTPConnectionPool('M_Server', port=8080, timeout=None, maxsize=3, block=True)
dashlet_thread_worker_pool_object = workerpool.WorkerPool(size=100)
#this section emulates a single manager calling 6 threads from the pool but limited to 3 TCP sockets by tcp_pool_object
for url in open("overview_urls.txt"):
job_object = Download_Dashlet_Job(url.strip())
dashlet_thread_worker_pool_object.put(job_object)
dashlet_thread_worker_pool_object.shutdown()
dashlet_thread_worker_pool_object.wait()