[英]how to terminate a thread which calls the webbrowser in python
我正在開發一個基於 linux 的應用程序,但現在我面臨着,因為我必須調用 webbrowser 來完成進一步的任務,但問題是程序卡住了並且不會終止。 我嘗試使用線程終止它,但它沒有收到中斷並且線程無限運行,下面是我正在嘗試的代碼的基本版本。希望你能解決我的問題,
import time
import threading
import webbrowser
class CountdownTask:
def __init__(self):
self._running = True
def terminate(self):
self._running = False
def run(self):
url='http://www.google.com'
webbrowser.open(url,new=1)
c = CountdownTask()
t = threading.Thread(target=c.run)
t.start()
time.sleep(1)
c.terminate() # Signal termination
t.join() # Wait for actual termination (if needed)
import time
import threading
import webbrowser
class CountdownTask(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self._running = True
def terminate(self):
self._running = False
def run(self):
url='http://www.google.com'
webbrowser.open(url,new=1)
t = CountdownTask()
t.start()
time.sleep(1)
t.terminate() # Signal termination
t.join() # Wait for actual termination (if needed)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.