加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

线程切换上的Python分段错误

发布时间:2020-12-20 13:14:19 所属栏目:Python 来源:网络整理
导读:我正在开发一个应该检查计算机是否能够ping google.com的应用程序.为此,我使用 python子进程模块并发出一个调用,如下面的代码所示: response = subprocess.call("ping -c 1 google.com -q",shell=True) 但是,运行一段时间后,程序退出并出现分段错误. 我有以
我正在开发一个应该检查计算机是否能够ping google.com的应用程序.为此,我使用 python子进程模块并发出一个调用,如下面的代码所示:

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,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就像我正在使用?在我看来,连续创建线程是造成这种分段错误的原因.

谢谢.

解决方法

你正在从计时器本身重新计时器,这可能不太好(用线程递归调用,argh).如果你对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个线程,每1秒创建一个ping进程(它不是一个受管制的计时器,但它应该足够了).

编辑:没有ping输出的版本:

import threading,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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读