每个线程递增并打印一个变量 运行() 函数给出奇怪的结果
Increment and print a variable every thread run() function gives weird results
我想在每次线程启动 运行() 函数时使用 count() 函数递增计数器变量。同一个线程从不同的模块调用一个函数并打印出应该分配给每个线程的计数器值。
事情是我在打印变量时得到了一个奇怪的结果,而不是得到 1、2、3 的列表......我得到了当前 运行ning 的线程总数。
这是我的线程模块:
import threading
import proxies
threads_list = []
good_counter = 0
run_counter = 0
worker = None
num_threads=10
timeout=10
class Worker(threading.Thread):
def __init__(self, timeout, proxy_list):
threading.Thread.__init__(self)
self.timeout = timeout
self.proxy_list = proxy_list
def run(self):
global good_counter
count()
proxy_ip = proxies.get_proxy(proxies.get_proxylist())
if proxies.is_proxy_good(proxy_ip):
good_count()
save_proxy(proxy_ip)
print('[+] HIT ! - %s' % (proxy_ip))
def set_threads(num_threads, timeout, proxy_list):
for i in range(num_threads):
worker = Worker(timeout, proxies.get_proxylist())
worker.setDaemon(True)
worker.start()
threads_list.append(worker)
def run_loop():
while proxies.proxy_list.qsize() > 0:
set_threads(num_threads, timeout, proxies.get_proxylist())
for item in threads_list:
item.join()
print('[!] Proxylist Qsize < 0 QUITTING ....')
def get_counter():
return run_counter
def count():
global run_counter
run_counter += 1
这是代理模块中使用的 is_proxy_good() 方法,只要有异常处理就会打印出 run_counter :
def is_proxy_good(proxy_ip):
try:
r = requests.get('https://www.example.com',proxies=proxy_ip,timeout=15,headers=headers)
if r.status_code is 200:
return True
return False
except requests.exceptions.Timeout:
print('N%d - %s - Proxy Timeout\n' % (threads.get_counter(),proxy_ip))
return False
except requests.exceptions.ProxyError:
print('N%d - %s - Proxy ProxyError\n' % (threads.get_counter(),proxy_ip))
return False
except requests.exceptions.SSLError:
print('N%d - %s - Proxy SSLError\n' % (threads.get_counter(),proxy_ip))
return False
except requests.exceptions.ConnectionError:
print('N%d - %s - Proxy ConnectionError\n' % (threads.get_counter(),proxy_ip))
return False
输出为:
N10 - {'https': 'https://x.xxx.xx.xxx:1080'} - Proxy ProxyError
N10 - {'https': 'https://x.xxx.xx.xxx:1080'} - Proxy ProxyError
N10 - {'https': 'https://x.xxx.xx.xxx:1080'} - Proxy ProxyError
..............
N20 - {'https': 'https://x.xxx.xx.xxx:1080'} - Proxy ProxyError
N20 - {'https': 'https://x.xxx.xx.xxx:1080'} - Proxy ProxyError
N20 - {'https': 'https://x.xxx.xx.xxx:1080'} - Proxy ProxyError
...........
为什么它从第一个线程开始打印线程总数?
我应该如何以正确的方式递增,以便打印出递增 1 的递增数字?
谢谢!
在您的代码中,run_counter
是一个全局变量。当您使用它时,它已经被您创建的所有线程所修改。您需要将值存储在您将要使用的线程实例持久存在的某个位置。我可能用这样的方法来处理它:
class Worker(thread):
_ids = count()
def __init__(self):
self.id = next(self._ids)
然后,在您的代理代码中的某处,您可以执行类似 getCurrentThead().id
.
的操作
我想在每次线程启动 运行() 函数时使用 count() 函数递增计数器变量。同一个线程从不同的模块调用一个函数并打印出应该分配给每个线程的计数器值。 事情是我在打印变量时得到了一个奇怪的结果,而不是得到 1、2、3 的列表......我得到了当前 运行ning 的线程总数。
这是我的线程模块:
import threading
import proxies
threads_list = []
good_counter = 0
run_counter = 0
worker = None
num_threads=10
timeout=10
class Worker(threading.Thread):
def __init__(self, timeout, proxy_list):
threading.Thread.__init__(self)
self.timeout = timeout
self.proxy_list = proxy_list
def run(self):
global good_counter
count()
proxy_ip = proxies.get_proxy(proxies.get_proxylist())
if proxies.is_proxy_good(proxy_ip):
good_count()
save_proxy(proxy_ip)
print('[+] HIT ! - %s' % (proxy_ip))
def set_threads(num_threads, timeout, proxy_list):
for i in range(num_threads):
worker = Worker(timeout, proxies.get_proxylist())
worker.setDaemon(True)
worker.start()
threads_list.append(worker)
def run_loop():
while proxies.proxy_list.qsize() > 0:
set_threads(num_threads, timeout, proxies.get_proxylist())
for item in threads_list:
item.join()
print('[!] Proxylist Qsize < 0 QUITTING ....')
def get_counter():
return run_counter
def count():
global run_counter
run_counter += 1
这是代理模块中使用的 is_proxy_good() 方法,只要有异常处理就会打印出 run_counter :
def is_proxy_good(proxy_ip):
try:
r = requests.get('https://www.example.com',proxies=proxy_ip,timeout=15,headers=headers)
if r.status_code is 200:
return True
return False
except requests.exceptions.Timeout:
print('N%d - %s - Proxy Timeout\n' % (threads.get_counter(),proxy_ip))
return False
except requests.exceptions.ProxyError:
print('N%d - %s - Proxy ProxyError\n' % (threads.get_counter(),proxy_ip))
return False
except requests.exceptions.SSLError:
print('N%d - %s - Proxy SSLError\n' % (threads.get_counter(),proxy_ip))
return False
except requests.exceptions.ConnectionError:
print('N%d - %s - Proxy ConnectionError\n' % (threads.get_counter(),proxy_ip))
return False
输出为:
N10 - {'https': 'https://x.xxx.xx.xxx:1080'} - Proxy ProxyError
N10 - {'https': 'https://x.xxx.xx.xxx:1080'} - Proxy ProxyError
N10 - {'https': 'https://x.xxx.xx.xxx:1080'} - Proxy ProxyError
..............
N20 - {'https': 'https://x.xxx.xx.xxx:1080'} - Proxy ProxyError
N20 - {'https': 'https://x.xxx.xx.xxx:1080'} - Proxy ProxyError
N20 - {'https': 'https://x.xxx.xx.xxx:1080'} - Proxy ProxyError
...........
为什么它从第一个线程开始打印线程总数? 我应该如何以正确的方式递增,以便打印出递增 1 的递增数字? 谢谢!
在您的代码中,run_counter
是一个全局变量。当您使用它时,它已经被您创建的所有线程所修改。您需要将值存储在您将要使用的线程实例持久存在的某个位置。我可能用这样的方法来处理它:
class Worker(thread):
_ids = count()
def __init__(self):
self.id = next(self._ids)
然后,在您的代理代码中的某处,您可以执行类似 getCurrentThead().id
.