Vairetz
Posts: 45
Joined: Wed Apr 27, 2016 4:54 pm

Pause a program in python

Sat Nov 16, 2019 10:47 am

Hi guys, I've wrote a code in python to automate my fish tank with raaspberry pi. All it works grate when I test it with small period of time but I need to run program for 2 week (or more) and pause the operations for 8/12 hours. When I pause It for long periods of time it stops and seems to close the program. I used the function pause() https://pypi.org/project/pause/ and also try the function time.sleep() and disable screensaver and power manager but nothing change.
How can I solve the problem?

User avatar
paddyg
Posts: 2602
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pause a program in python

Sat Nov 16, 2019 12:17 pm

Is your code on a repository so we can look at it? With control programs generally I wouldn't use sleep or pause, apart from for a second or two every loop. I would check the time against a trigger time for the next action. That's kind of what the pause module intends to do but it might have lots of additional stuff that conflicts with the way you've written your controller.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
thagrol
Posts: 4040
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Pause a program in python

Sat Nov 16, 2019 2:44 pm

There's a couple of ways I'd tacckle that:
    • Remove the loop/pause/time.sleep from your code so it runs once then exits.
    • Use cron to run your program at the required interval. See

      Code: Select all

      man cron
      or the internet for more details.
  1. Use the pause function from the signal module (as used in the gpiozero examples) to suspend your main thread then use the Timer object from the threading module to run your stuff after your desired elapsed time.

    You'll need to remove any loop/pause/time.sleep and wrap the code that does stuff in a function. That function will need to (re)start the timer so it gets called again. You'll also need some exception handling to stop any running timer on exit.

    I posted a simple example here: https://www.raspberrypi.org/forums/view ... 7#p1558269
Arguing with strangers on the internet since 1993.

User avatar
rpiMike
Posts: 1614
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Pause a program in python

Sat Nov 16, 2019 3:37 pm

Another example using current datetime:

https://www.raspberrypi.org/forums/view ... 5#p1565980

pfletch101
Posts: 633
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Pause a program in python

Mon Nov 18, 2019 4:50 pm

I think that you need to try to find out the real reason why your program is exiting/failing. I don't think that the problem is how it is pausing/sleeping. I have a number of 24/7 services that use time.sleep() to pause execution for long periods of time (more than 12 hours for some of them), without any issues. Indeed, most of them use a function to which I pass the desired wakeup time, and which then calculates the number of seconds to sleep before sleeping for that time (I am aware that the precision of this approach is limited, and this is not critical for my applications).

Vairetz
Posts: 45
Joined: Wed Apr 27, 2016 4:54 pm

Re: Pause a program in python

Fri Dec 06, 2019 4:42 pm

Sorry for the long time without answer. I notice that the program crash only if I start it from SSH connection, If I start it via vnc or "directly" from raspberry all it works well. Anyone have notice this problem before? (I haven't changed anything in the code)

User avatar
thagrol
Posts: 4040
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Pause a program in python

Fri Dec 06, 2019 5:50 pm

Vairetz wrote:
Fri Dec 06, 2019 4:42 pm
Sorry for the long time without answer. I notice that the program crash only if I start it from SSH connection, If I start it via vnc or "directly" from raspberry all it works well. Anyone have notice this problem before? (I haven't changed anything in the code)
How exactly are you starting it?

Are you leaving the ssh connection open or closing it? Does the remote machine (the one running the ssh client) sleep/hibernate when idle? Reboot around the time your program is crashing?

Foreground programs will be closed if/when you close the ssh session. Background programs will likewise be closed unless started with "nohup". See "man nohup" for how to do that.

It's possible depending on how your VNC installtion is configured that close it on the remote end doesn't close the session on the pi end so the terminal session and everything running in it stays open.

Locally on the Pi? logging out of the desktop will kill everything as above as will closing the terminal session. If you left the terminal open and just turned off the screen it wil stay running.

I suspect the problem isn't your code but how you're invoking it.
Arguing with strangers on the internet since 1993.

Vairetz
Posts: 45
Joined: Wed Apr 27, 2016 4:54 pm

Re: Pause a program in python

Sat Dec 07, 2019 5:27 pm

thagrol wrote:
Fri Dec 06, 2019 5:50 pm

How exactly are you starting it?

Are you leaving the ssh connection open or closing it? Does the remote machine (the one running the ssh client) sleep/hibernate when idle? Reboot around the time your program is crashing?

Foreground programs will be closed if/when you close the ssh session. Background programs will likewise be closed unless started with "nohup". See "man nohup" for how to do that.

It's possible depending on how your VNC installtion is configured that close it on the remote end doesn't close the session on the pi end so the terminal session and everything running in it stays open.

Locally on the Pi? logging out of the desktop will kill everything as above as will closing the terminal session. If you left the terminal open and just turned off the screen it wil stay running.

I suspect the problem isn't your code but how you're invoking it.
I used to close the ssh connection after start the python script, now I understand the reason why the Pi closed the program. Thank you very much for help!

Return to “Python”