我有一个python脚本,所以我在python中使用线程模块来并发执行。
class check_bl(threading.Thread):
def __init__(self, ip_client, reverse_ip, key, value):
threading.Thread.__init__(self)
self.ip_client = ip_client
self.reverse_ip = reverse_ip
self.key = key
self.value = value
def run(self): db = MySQLdb.connect('localhost','mytable','user','mytable') cursor = db.cursor() query =“dig + short”+ str(reverse_ip)+“。” +键 尝试: output = subprocess.check_output(query,shell = True) output_edited = output.strip() 除了: output_edited =“超时发生”
if output_edited in value:
try:
cursor.execute() # execute INSERT command
db.commit()
except:
db.rollback()
db.close()
else:
try:
cursor.execute() # execute INSERT command
db.commit()
except:
db.rollback()
db.close()
对于IPNetwork中的ip_client(range_ip_client): ip_client = .... reverse_ip = ...
for key, value in my_dictionary.items():
myThread = check_bl(ip_client, reverse_ip, key, value)
myThread.start()
threadList.append(myThread)
for t in threadList:
t.join()
问题是每个都有一些关键,my_dictionary中的值没有执行(不是INSERT到数据库,虽然有些键,值执行多次)。 我的字典有30个项目,但只有10到20个执行。如果我像下面的例子那样插入join(),那么我的所有字典item()都会执行,但同时只执行一个线程。 创建的每个线程都有问题,它会创建数据库连接。所以数据库无法处理它。
for key, value in my_dictionary.items():
myThread = check_bl(ip_client, reverse_ip, key, value)
myThread.start()
threadList.append(myThread)
for t in threadList:
t.join()
或
for key, value in my_dictionary.items():
myThread = check_bl(ip_client, reverse_ip, key, value)
myThread.start()
t.join()
感谢Dksj,我的问题解决了。也许类方法有问题,只需要定义一个函数。
def check_bl(ip_client, reverse_ip, key, value):
db = MySQLdb.connect('localhost', 'mytable', 'user', 'mytable')
cursor = db.cursor()
query = "dig +short " + str(reverse_ip) + "." + key
try:
output = subprocess.check_output(query, shell=True)
output_edited = output.strip()
except:
output_edited = "Timeout occured"
if output_edited in value:
try:
cursor.execute() # execute INSERT command
db.commit()
except:
db.rollback()
db.close()
else:
try:
cursor.execute() # execute INSERT command
db.commit()
except:
db.rollback()
db.close()
for ip_client in IPNetwork(range_ip_client):
ip_client = ....
reverse_ip = ...
for key, value in my_dictionary.items():
myThread = check_bl(ip_client, reverse_ip, key, value)
threadList.append(myThread)
[t.start() for t in threadList]
[t.join() for t in threadList]
答案 0 :(得分:0)
我的问题与你的问题略有不同,我没有使用类方法 我刚刚修改了你的代码,你可以尝试一下。这个链接帮助我解决了我的问题:How to use threading in Python?
def check_bl(ip_client,reverse_ip,key,value): ....
threads []
for key, value in my_dictionary.items():
# Instantiates the thread
myThread = Thread( target=check_bl, args=(ip_client, reverse_ip, key, value) )
#add thread to list
threads.append(myThread)
[th.start() for th in threads]
#Join all the threads to make sure the parent waits for all of them to finish
[th.join() for th in threads]