how to terminate a thread which calls the webbrowser in python

安稳与你 提交于 2020-01-05 07:11:11

问题


I am developing an app which is linux based but right now I am facing as I have to call the webbrowser to do the further task but the problem is the program gets stuck and does not terminate. I have tried to terminate it using thread but it doesn't receive the interrupt and the thread runs infinitely ,below is the basic version of the code I was trying.Hope you got my problem ,

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)

回答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)


来源:https://stackoverflow.com/questions/37152387/how-to-terminate-a-thread-which-calls-the-webbrowser-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!