队列工作者在一段时间后以多个队列作为输入静默退出

时间:2018-08-22 18:45:53

标签: python multithreading queue

首先,我是一个初学者。香港专业教育学院试图使用带有多个输入的pythons队列功能,但它仅适用于前几个帐户,然后不再适用(变量ID卡在相同的数字上)。现在已经为此奋斗了一段时间。它是一个简单的多线程帐户检查器。

original_ip = Proxies.get_external_ip()

class CheckThread(threading.Thread):
def __init__(self, id, account_queue, proxy_queue, out_queue):
    threading.Thread.__init__(self)
    self.account_queue = account_queue
    self.proxy_queue = proxy_queue
    self.out_queue = out_queue
    self.id = id
    self.original_ip = original_ip

def run(self):
    while True:
        print(str(self.proxy_queue.qsize()))
        if self.proxy_queue.qsize() == 0:
            print("No Proxy, Worker " + self.id)
            break

        account = self.account_queue.get()
        proxy = self.proxy_queue.get()
        print("Using proxy " + proxy)


        proxset = self.get_proxy(account, proxy) # Returns True/False depends on if proxy is valid

        if proxset is None: # proxy error
              print(proxy + " is not valid!")
              self.proxy_queue.task_done()
              continue

  def get_proxy(self, account, proxy):
        account_proxy = account.split('|')[3]

        if account_proxy:
            if Proxies.check_proxy(self, account_proxy):
                return account
            else:
                if proxy:
                    if Proxies.check_proxy(self, proxy):
                        return account.replace(account_proxy, proxy)
                    else:
                        return None

        else:
            if proxy:
                if Proxies.check_proxy(self, proxy):
                    return account.replace(account_proxy, proxy)
                else:
                    return None

我开始做这件事的方式:

def main():
thread_number = 5

accounts = queue.Queue()
for row in tuple(open(accountlist, "r")):
    accounts.put(row.rstrip())
proxies = queue.Queue()
for row in tuple(open(proxylist, "r")):
    proxies.put(row.rstrip())

result_queue = queue.Queue()

workers = []
if thread_number > proxies.qsize():
    thread_number = proxies.qsize()
for i in range(0, thread_number):
    t = CheckThread(i, accounts, proxies, result_queue)
    t.start()
    workers.append(t)

accounts.join()

while not result_queue.empty():
    print("Success:" + result_queue.get())

for worker in workers:
    worker.terminate()

if __name__ == '__main__':
    main()

尽管我认为这不是必需的,但由于我仅检查代理队列atm,因此我暂时不接触out_queue或account_queue。代理是IP:端口格式和帐户存储在字符串中,并在处理之前从我的列表中读出。据我所知,代理检查功能可以正常工作。

任何帮助表示赞赏

0 个答案:

没有答案