Python 完成某个任务后停止线程
所以我写了一些代码,当按下停止按钮时停止线程。它是有效的,但有一些警告。它倾向于在一两秒钟内冻结资源,一旦按下它就会停止一切。我想停止按钮停止线程一旦它完成了它正在做的事情 以下是我为停止事件创建的函数:Python 完成某个任务后停止线程,python,multithreading,python-2.7,selenium,selenium-chromedriver,Python,Multithreading,Python 2.7,Selenium,Selenium Chromedriver,所以我写了一些代码,当按下停止按钮时停止线程。它是有效的,但有一些警告。它倾向于在一两秒钟内冻结资源,一旦按下它就会停止一切。我想停止按钮停止线程一旦它完成了它正在做的事情 以下是我为停止事件创建的函数: def Stop(self, event): if myClass.worker != None: if myClass.browser!=None: while(myClass.browser.quit()!=None):
def Stop(self, event):
if myClass.worker != None:
if myClass.browser!=None:
while(myClass.browser.quit()!=None):
myClass.browser.quit()
myClass.worker.stop()
myClass.worker = None
while True:
try:
myfile = open(self.fpath, "r+")
myfile.close()
break
except IOError:
myClass.closeIt(self)
os.unlink(self.fpath)
os.rename(self.temp, self.fpath)
基本上,如果没有生成线程,那么它就不是无线程,它会退出浏览器窗口,然后停止从浏览器上的网页中抓取信息的线程。然后保存要写入的文件
实际停止的线程是我创建的openExcel类的一个实例:
myClass.worker = openExcel(temp)
此类的实例调用一个名为findDomains的函数:
def findDomains(self,searchrow, numrows, sheet, wbook):
for i in range(searchrow, numrows):
domain = sheet.cell_value(i, 2)
if domain == "":
company = sheet.cell_value(i, 1)
self.browser.get('https://www.google.com/')
wait = WebDriverWait(self.browser, 300)
inputGoogle=wait.until(EC.presence_of_element_located((By.NAME, "q")))
inputGoogle.clear()
inputGoogle.send_keys(company)
inputGoogle.submit()
self.browser.refresh()
tries = 0
while tries<1000:
try:
domain=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "cite._Rm")))
domain = domain.text
break
except StaleElementReferenceException:
tries+=1
self.browser.refresh()
domainlength=len(domain)
period=0
for charac in range(domainlength):
if domain[charac]==".":
period+=1
if period==1:
if "//" in domain:
domain = domain.split("//",1)[1]
index = domain.find("/")
domain = domain[:index]
elif period > 1:
domain = domain.split(".",1)[1]
index = domain.find("/")
domain = domain[:index]
checkdom=domain.split(".",1)[0]
if (checkdom+".") not in domain:
domain="error"
worksheet.write(i,2,domain)
wbook.save(file)
我不想停止它在这个函数中所做的一切,而是希望停止功能等到域被保存到excel工作表中。也就是说,在试图关闭浏览器并退出之前,让它完成正在做的事情。您好,您能否尝试通过用实体模型替换代码中大部分不相关的部分来复制您遇到的问题?这个练习可能会帮助你发现它到底出了什么问题!a实际上这是一个MCVE。。。最小的、完整的、可验证的示例:@zmo这里是所有的代码:压缩代码是不可能的。