how to terminate a thread which calls the webbrowser in python
Asked Answered
N

1

1

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)
Northman answered 11/5, 2016 at 3:2 Comment(2)
t.terminate() ... but afaik it only works on linuxAnastice
thanks I got the trick,I did a little modification ,i used the Countdowntask as thread and then t.terminate was working perfectly.class CountdownTask(threading.Thread):Northman
N
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)
Northman answered 11/5, 2016 at 3:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.