多线程遇到的问题:
import concurrent.futures
import importlib
import queue
import itertools
import threading
import time
class ThreadPool():
def __init__(self,thread):
self.max_thread = thread
self.thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=self.max_thread)
self.task_queue = queue.Queue()
self.futures = {}
'''
module:字符串,模块名称
function:字符串,函数名
usernamelist:用户名列表
passwordlist:密码列表
'''
def add_port_brute_task(self,module,function,host,port,usernamelist,passwordlist):
combinations = list(itertools.product(usernamelist, passwordlist))
for combination in combinations:
username, password = combination
new_task=(module,function,host,port,username,password)
self.task_queue.put(new_task)
def start_port_brute_threadpool(self):
start_time=time.time()
while self.task_queue.qsize() != 0:
module,function, host, port, username, password=self.task_queue.get()
args=(host,port,username,password)
future=self.thread_pool.submit(self.run_poc,module,function,args)
self.futures[future] = (function, args)
self.thread_pool.shutdown(wait=True)
print("\n用时:%.2f 秒" % (time.time() - start_time))
'''
通过importlib获取到模块,获取函数直接执行
'''
def run_poc(self,module,func,args):
module=importlib.import_module(module)
function=getattr(module,func)
result=function(*args)
if result==True:
self.thread_pool.shutdown(wait=False)
我在其它函数中创建这个ThreadPool的类,并且调用start_port_brute_threadpool()执行一个函数的时候,会出现这个同样的password数据被执行了很多次的情况,产生的原因是什么?我只想每个password只出现一次,并且按顺序来跑。