我正在开发一个基于Linux的应用程序,但现在我面临着,因为我必须调用网络浏览器来完成进一步的任务,但问题是程序卡住了并且不会终止。我尝试使用线程终止它,但它没有收到中断并且线程无限运行,下面是我正在尝试的代码的基本版本。希望您能解决我的问题,
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)
关于python - 如何终止在Python中调用Web浏览器的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37152387/