Python线程切换时出现分段错误

4

我正在开发一个应用程序,它应该检查计算机是否能够ping通google.com。为了做到这一点,我使用python的subprocess模块,并通过以下代码调用:

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

然而,程序运行一段时间后会出现分段错误。

以下是我的代码:

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

在 GDB 上运行时,我遇到了分割错误并得到了以下跟踪信息:
--- 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.Timer? 在我看来,线程的连续创建导致了这个分段错误。

谢谢。


奇怪。你可以删除shell=True参数,因为它没有被使用。 - Jean-François Fabre
@Jean-FrançoisFabre -- 去掉 shell=True 是好的,但是你应该将参数作为列表而不是字符串传递。 - mgilson
这些计时器线程不应该在某个时候被 .join 吗? - mgilson
@mgilson 我对线程还很陌生,我在网上看到的大多数示例都没有使用.join。也许我错过了什么,我会研究一下。谢谢! - Rodrigo Meurer
1个回答

0
你正在计时器内部重新设置计时器,这可能不太好(使用线程进行递归调用,呃)。如果你对ping延迟的准确性要求不高,实际上并不需要Timer:你可以启动一个循环并定期像这样对谷歌进行ping的线程。
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进程(这不是一个规定的计时器,但应该足够)。

编辑:没有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()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接