简体   繁体   English

如何退出树莓派上的python程序

[英]How to exit python program on raspberry

My program in python runs on RaspBerry Pi, and instantiates several objects (GPIO inputs and outputs, http server, webSocket, I2C interface, etc..., with thread) .我的 Python 程序在 RaspBerry Pi 上运行,并实例化了几个对象(GPIO 输入和输出、http 服务器、webSocket、I2C 接口等...,使用线程)

When exiting my program, I try to clear all the resources, and delete all the instances.退出程序时,我尝试清除所有资源,并删除所有实例。 For the network objects, I close listening sockets and so on.对于网络对象,我关闭侦听套接字等。 I finish with a sys.exit() call, but program doe not exit and does not returns alone to linux console (I need to press ctrl+z).我完成了sys.exit()调用,但程序没有退出,也没有单独返回到 linux 控制台(我需要按 ctrl+z)。

Are there some objects that are not released, how to know, and how to force exit ?是否有一些对象没有被释放,如何知道,以及如何强制退出?

Best regards.此致。

I'm programming for Raspberry Pi with the Kivy library, and I've had a similar problem.我正在使用 Kivy 库为 Raspberry Pi 编程,我遇到了类似的问题。 The comments in this topic have helped me solve it.该主题中的评论帮助我解决了它。

In my case, Kivy uses "Clock()" objects to call a function with certain time intervals, providing the main program loop.就我而言,Kivy 使用“Clock()”对象以特定时间间隔调用函数,提供主程序循环。

As it turns out, while everything works fine on a PC (in Linux or Windows), on Raspberry Pi you have to manually stop all program loops, otherwise "sys.exit()" will not work.事实证明,虽然在 PC 上一切正常(在 Linux 或 Windows 中),但在 Raspberry Pi 上,您必须手动停止所有程序循环,否则“sys.exit()”将无法工作。

At first, I had "sys.exit()" command executing at key press.起初,我在按键时执行了“sys.exit()”命令。 That didn't work for Raspberry.这对 Raspberry 不起作用。 So, instead I used a global variable that would change value when the exit key is pressed, and had its value checked within the program loop, where I then called "sys.exit()" (and "return False", which signals Kivy to destroy the Clock() object).因此,我使用了一个全局变量,当按下退出键时它会改变值,并在程序循环中检查它的值,然后我在那里调用“sys.exit()”(和“return False”,这表示 Kivy销毁 Clock() 对象)。

I've also been using separate Clock() objects for some animations in my program, and I've noticed that if I press exit while an animation is running, my program would freeze without exiting - just as before, because "sys.exit()" was called while some Clock() objects were running.我也一直在我的程序中为一些动画使用单独的 Clock() 对象,我注意到如果我在动画运行时按退出,我的程序将冻结而不退出 - 就像以前一样,因为“sys.exit ()" 在某些 Clock() 对象运行时被调用。

The bottom line is - if you have trouble with "sys.exit()" on Raspberry Pi, make sure all program loops in your code are stopped before calling sys.exit().底线是 - 如果您在 Raspberry Pi 上遇到“sys.exit()”问题,请确保在调用 sys.exit() 之前停止代码中的所有程序循环

As the most simple example, if you have a program running a loop like作为最简单的例子,如果你有一个程序运行像

while True:

instead use而是使用

while running:
   # where running = True

then change to "running = False" before calling "sys.exit()".然后在调用“sys.exit()”之前更改为“running = False”。

I had a similar problem programming a simple GPIO app on the Pi.我在 Pi 上编写一个简单的 GPIO 应用程序时遇到了类似的问题。 I was using the GPIOZero library, and as their code examples suggest, I was waiting for button pushes using signal.pause() .我正在使用 GPIOZero 库,正如他们的代码示例所暗示的那样,我正在使用signal.pause()等待按钮按下。 This would cause the behavior you describe - even sys.exit() would not exit!这会导致您描述的行为 - 即使sys.exit()也不会退出!

The solution was, when it was time for the code to finish, to do this:解决方案是,当代码完成时,执行以下操作:

# Send a SIGUSER1 signal; this will cause signal.pause() to finish. os.kill(os.getpid(), signal.SIGUSR1)

You don't even have to define a signal handler if you don't mind the system printing out "User defined signal 1" on the console.如果您不介意系统在控制台上打印出“用户定义的信号 1”,您甚至不必定义信号处理程序。

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM