I have a python script, so I use threading module in python to execute concurrently.
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) + "." + 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)
myThread.start()
threadList.append(myThread)
for t in threadList:
t.join()
The problem is each some key, value in my_dictionary did not execute(not INSERT to database, although some key, value execute more than one time). My dictionary has 30 items, but just 10 - 20 of them execute. If I insert join() like 2 example below, all my dictionary item() execute but at the same time, just one thread execute. Is there something wrong for every threads created, it create an database connection. So database can't handle it.
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()
or
for key, value in my_dictionary.items():
myThread = check_bl(ip_client, reverse_ip, key, value)
myThread.start()
t.join()
Thanks Dksj, my problem is solved. Maybe something wrong with class method, just need to define a function.
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]