On Sunday 22 November 2015 05:24 PM, Laura Creighton wrote:
In a message of Sun, 22 Nov 2015 11:23:58 +0530, CMG Thrissur writes:
Hello, I have seen some examples of terminating a thread the safe way. But is there a way to kill a thread without worrying about what happens next. Like thread.stop() or something George
import sys sys.exit(1) This will brutally kill the whole process. Sometimes that is what you want. Most of the time, it isn't. Python doesn't have a way to grab a thread and kill it, it can only ask it to please stop themselves. Laura

Thank you Steven and Laura for your reply. I was testing some urls with pythonwhois, and i got stuck with some threads taking longer than expected time, i found a roundabout way for it. Below is my code, do you suggest any improvements:

import datetime import time import mysql.connector import threading import pythonwhois cnx = mysql.connector.connect(user='root',password=pas,host='localhost',database=db) cursor = cnx.cursor() def ProcessList(domain:str): ''' :paramdomain:din, dcom etc :return: ''' query='select url from urls where (%s="" or %s<curdate()) and %s<curdate()-1'%(domain,domain,domain+'lastcheck') print (query) cursor.execute(query) tmplist=[] for url in cursor: tmplist.append(url[0]) return tmplist #print (ProcessList('din')) class DomainUrlValidityChecker(threading.Thread): def __init__(self,domain:str,urllist:list): threading.Thread.__init__(self) print ("Starting",self.name) self.domain=domain self.urllist=urllist self.time=datetime.datetime.now() def run(self): for url in self.urllist: workurl = url + '.' + self.domain result = pythonwhois.get_whois(workurl) if result.get('expiration_date', None) != None: #print(workurl, result['expiration_date'][0].date().isoformat()) #print(result['expiration_date'][0].year) query="update urls set d%s='%s',d%s=curdate() where url='%s';"%(self.domain,result['expiration_date'][0].date().isoformat(),self.domain+'lastcheck',url) print(query) cnx = mysql.connector.connect(user='root',password=pas,host='localhost',database=db) cursor = cnx.cursor() cursor.execute(query) cnx.commit() else: #print('Available', workurl) query="update urls set d%s='',d%s=curdate() where url='%s';"%(self.domain,self.domain+'lastcheck',url) print(query) cnx = mysql.connector.connect(user='root',password=pas,host='localhost',database=db) cursor = cnx.cursor() cursor.execute(query) cnx.commit() print ("Ending",self.name) class Sleeper(threading.Thread): def __init__(self,sleepcounter): threading.Thread.__init__(self) self.sleepinterval=sleepcounter def run(self): time.sleep(self.sleepinterval) if __name__=="__main__": urllist=ProcessList('dus') #specifying domain table column print (urllist) counter=0 tdic={} runcounter=10000 while runcounter>0: print ('loop',runcounter,'total count',len(threading.enumerate()),'total of value', len(tdic.keys()),datetime.datetime.now().time()) if len(tdic.keys())<10: #changed to consider on keys while len(tdic.keys())<10: # same as above wlist=urllist[counter:counter+10] print ('for new thread pass list',wlist) tdic[counter]=DomainUrlValidityChecker('us',wlist) #specifying actual domain tdic[counter].start() counter+=10 else: tmpitems=list(tdic.keys()) for item in tmpitems: #print(tdic[item].name) if tdic[item].isAlive()==False: print ('deleting dead',item,tdic[item].name) del tdic[item] continue if (datetime.datetime.now()-tdic[item].time).total_seconds()>30: print ('deleting long overdue ',item,tdic[item].name) del tdic[item] for item in tdic.keys(): print (tdic[item].name) print ("sleeping") slth=Sleeper(10) slth.start() slth.join() print ('awake') runcounter-=1


Thanks

George
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to