1

我正在开发一个基于 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)
4

1 回答 1

1
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)
于 2016-05-11T03:53:10.743 回答