繁体   English   中英

如何终止在python中调用webbrowser的线程

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM