2017-06-15 12 views
0

2つのホストのリストが利用可能な1つのプログラムを作成したいと思います。私は各ホストからデータを読みたい。それは約5-10秒かかるので、私は別のスレッドで各ホストのデータを読み取るしたいです。Python:マルチスレッドのPythonプログラムを終了するには?

私は以下のコードを作成しましたが、私の期待通りに機能していますが、Ctrl + cを押してプログラムが終了しなかった場合にのみ問題が発生します。

マイコード:私はこのプログラムにCtrl + Cを押した後に終了することができますどのように

import threading 
import time,os,sys 
import signal 

is_running = True 

def signal_handler(signal, frame): 
    print "cleaning up...please wait..." 
    v1.stop() 
    v2.stop() 
    global is_running 
    is_running = False 

class Thread2(threading.Thread): 
    def __init__(self, function,args): 
     self.running = False 
     self.function = function 
     self.args = args 
     super(Thread2, self).__init__() 

    def start(self): 
     self.running = True 
     super(Thread2, self).start() 

    def run(self): 
     while is_running: 
      self.function(self.args) 
      time.sleep(time_interval) 

    def stop(self): 
     self.running = False 

def b_iterate(hostnames): 

    for host_name in hostnames: 
     v = Thread2(function = read_cet_data,args = host_name) 
     v.start() 

def read_b_data(host): 

    # 
    #reading some data from current host (5-10 seconds processing) 
    # 
    #here, this thread is not neccessary, want to stop or kill or terminate it 
    if threading.current_thread().isAlive(): 
     threading.current_thread().stop() 

def a_iterate(entp_hostnames): 

    for host_name in entp_hostnames: 
     v = Thread2(function = read_entp_data,args = host_name) 
     v.start() 

def read_a_data(host): 

    # 
    #reading some data from current host (5-10 seconds processing) 
    # 
    #here, this thread is not neccessary, want to stop or kill or terminate it 
    if threading.current_thread().isAlive(): 
     threading.current_thread().stop() 

if __name__ == "__main__": 

    signal.signal(signal.SIGINT, signal_handler) 
    #a_hostnmaes & b_hostnmaes are the lists of hostnames 
    v1 = Thread2(function = a_iterate,args = a_hostnames) 
    v2 = Thread2(function = b_iterate,args = b_hostnames) 
    v1.start() 
    v2.start() 
    while is_running: 
     pass 

。何か不足していますか?

+0

チェック:https://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in-pythonとhttps://のstackoverflow。 com/questions/18114560/python-catch-ctrl-c-command-prompt-really-want-to-quit-yn-resume-executi – Drako

+0

@ Drako-私はそれをチェックしました。私のプログラムは複数のスレッドのために終了しませんでした。どのように私はそれを終了するように通知することができます。 – kit

+0

申し訳ありませんが、私は何もマルチスレッド、一スレッドでうまく動作する軽量アプリケーション:)を必要としていない - その経験は、ちょうどそれらのリンクがアイデアを助けることができると期待して – Drako

答えて

0

コントロールCですべてを終了させたい場合は、スレッドで停止機能を使用する必要はありません。あなたはそれらをdaemoniseことができます。

v1 = Thread2(function = a_iterate,args = a_hostnames) 
v2 = Thread2(function = b_iterate,args = b_hostnames) 
v1.daemon = True 
v2.daemon = True 
v1.start() 
v2.start() 

とすぐにあなたのメインプログラムが死んだとして、これらのスレッドが同様に死にます。 .daemon = Trueをコード内のスレッドが作成される他のすべての場所に追加する必要があります。

ハンヌ

+0

@ Hannu - あなたの答えをありがとう。私は小さな疑いがある。 read_b_data関数とread_a_data関数で、このデーモンスレッドをどのように終了または停止できますか? – kit

0

することはできいずれかのように、別のスレッドがそれを検出することができフラグと終了

または

  • を設定し、メインスレッド
    • キャッチKeyboardInterruptキャッチをキャッチardInterrupt
    • コールos._exit()この
関連する問題