How to interrupt a thread that calls a webbrowser in python

I am developing a Linux-based application, but right now I am faced with the fact that I need to call a web browser to perform a further task, but the problem is that the program is stuck and does not end. I tried to finish it with a thread, but it does not receive an interrupt, and the thread runs endlessly, below is the basic version of the code I tried. 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
source share
1 answer
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)
+1
source

Source: https://habr.com/ru/post/1674779/


All Articles