2016-05-11 12 views
1

私はLinuxベースのアプリを開発していますが、私は直面していますが、Webブラウザを呼び出して別のタスクを実行する必要がありますが、問題はプログラムがスタックしてしまいます。終了しません。 `、webbrowserをPythonで呼び出すスレッドを終了するには

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) 
+0

以下、私はあなたが私の問題を抱えてtrying.Hopeたコードの基本的なバージョンがあり、私はスレッドを使用して、それを終了しようとしたが、それは、割り込みを受信しないとスレッドが無限に実行されますt.terminate() '...しかし、それはLinux上でしか動作しません。 –

+0

私はちょっとした改造をしました。少し修正しました。スレッドとして' Countdowntask'を使い、その後、t.terminateが完全に動作していました。 'class CountdownTask threading.Thread): ' –

答えて

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) 
関連する問題