2016-10-07 9 views
4

私は、コンピュータがgoogle.comにpingできるかどうかを確認するアプリケーションを開発しています。スレッドスイッチのPythonセグメント化エラー

response = subprocess.call("ping -c 1 google.com -q", shell=True) 

は、しかし、いくつかの時間のために実行した後に、プログラムがセグメンテーションフォールトで終了します。次のコードのように私はPythonのsubprocessモジュールを使用してコールを発行することをすることができません。

私は次のコードを持っている:私は、セグメンテーションの際に以下のトレースを取得GDB上でこれを実行する

daemon.py

def dataset_save(smartphone, mote): 
print("DATA LOGGER:\tStarting Now") 
with open('dataset.csv', 'a+') as dataset: 
    dataset.write(str(datetime.datetime.today()) + ',' + \ 
      str(datetime.datetime.today().weekday()) + ',' + \ 
      str(smartphone.connected) + ',' + \ 
      str(mote.A0_pw) + ',' + \ 
      str(mote.B00_pw) + ',' + \ 
      str(mote.B01_pw) + ',' + \ 
      str(mote.B10_pw) + ',' + \ 
      str(mote.B11_pw) + ',' + \ 
      str(presence.get_value()) + ',' + \ 
      str(temperature.get_value()) + ',' + \ 
      str(luminosity.get_value()) + '\n') 
    print("DATA LOGGER: \tData successfully logged @ %s!" %str(datetime.datetime.today())) 
return 


def run(): 
    check_internet() 
    while True: 
     dataset_save(smartphone, gateway) 
     check_presence() 

check_internet.py

def check_internet(): 
response = subprocess.call("ping -c 1 google.com -q", shell=True) 
print(response) 
if response == 0: 
    print ("CONNECTIVITY: \tConnected to internet") 
    threading.Timer(1, check_internet).start() 
    return 

else: 
    print("CONNECTIVITY: \tUnable to connect to internet") 
    threading.Timer(1, check_internet).start() 
    return 

を障害:

--- google.com ping statistics --- 
1 packets transmitted, 1 received, 0% packet loss, time 0ms 
rtt min/avg/max/mdev = 146.626/146.626/146.626/0.000 ms 
0 
CONNECTIVITY: Connected to internet 
[New Thread 0xb55ffb40 (LWP 4064)] 

Program received signal SIGSEGV, Segmentation fault. 
[Switching to Thread 0xb65ffb40 (LWP 4043)] 
PING google.com (216.58.222.110) 56(84) bytes of data. 
__deallocate_stack (pd=0xb65ffb40) at allocatestack.c:760 
760 allocatestack.c: No such file or directory. 
(gdb) 
--- google.com ping statistics --- 
1 packets transmitted, 1 received, 0% packet loss, time 0ms 
rtt min/avg/max/mdev = 146.504/146.504/146.504/0.000 ms 

(gdb) bt 
#0 __deallocate_stack (pd=0xb65ffb40) at allocatestack.c:760 
#1 0xb7fc3eab in start_thread (arg=0xb65ffb40) at pthread_create.c:427 
#2 0xb7e8164e in clone() at ../sysdeps/unix/sysv/linux/i386/clone.S:129 
(gdb) 

私はthreding.Thisを使用してはいけない理由はありますか?スレッドの連続的な作成がこのセグメンテーション違反の原因となっているようです。

ありがとうございました。

+0

奇妙な。 'shell = True'パラメータは、使用されていないので削除することができます。 –

+0

@ Jean-FrançoisFabre - 'shell = True'を削除すると良いでしょうが、文字列ではなくリストとして引数を渡す必要があります。 – mgilson

+0

これらのタイマースレッドは、ある時点で '.join'edになってはなりませんか? – mgilson

答えて

0

タイマー自体からタイマーをリセットしていますが、それはあまり良くないかもしれません(スレッドを使った再帰呼び出し、その他)。あなたがあまりにもpingの遅延精度については要求していない場合は、本当にTimerを必要としません:

import threading,subprocess,time 

def check_internet(): 
    while True: 
     time.sleep(1) 
     response = subprocess.call("ping -n 1 google.com".split()) 
     print(response) 
     if response == 0: 
      print("CONNECTIVITY: \tConnected to internet") 
     else: 
      print("CONNECTIVITY: \tUnable to connect to internet") 

t=threading.Thread(target=check_internet) 
t.start() 

print("Started") 
t.join() 

あなただけの1つのpingを作成する1つのスレッドを作成:あなたがループし、pingが、このように定期的にgoogleのスレッドを開始することができます1秒ごとに処理します(調整されたタイマーではありませんが、それで十分です)。

EDIT:ping出力なしのバージョン:

import threading,subprocess,time 

def check_internet(): 
    while True: 
     time.sleep(1) 
     p = subprocess.Popen("ping -n 1 google.com".split(),stdout=subprocess.PIPE,stderr=subprocess.STDOUT) 
     out,err = p.communicate() 
     response = p.wait() 
     if response == 0: 
      print("CONNECTIVITY: \tConnected to internet") 
     else: 
      print("CONNECTIVITY: \tUnable to connect to internet") 

t=threading.Thread(target=check_internet) 
t.start() 

print("Started") 
t.join() 
関連する問題