Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5013

Python • Re: Change sleep duration of a loop without restarting it (use a variable from an imported script?)

$
0
0
Could I have it import the variable from another script that I could update without interrupting the running code?
Just check if a text file exists at the end of time.sleep().
If it does set SLEEPDUR = 43200
else set SLEEPDUR = 3600.

Code:

from pathlib import Pathmy_file = Path("/path/to/file")if my_file.is_file():SLEEPDUR = 43200else:SLEEPDUR = 3600
So to create the file just use touch from the command line
Then when you are ready just delete it.
I could do multiple "elif" lines with a variety of SLEEPDUR values. I could create the files (likely naming them the desired values) and move them in and out of the folder for them to be seen. The only issue I see is that if it is on a 6 hour cycle, then I would have to wait until that cycle is done for it to be changed to a different SLEEPDUR value.
Are you sitting down? You could (gasp!) put the desired sleep time value in the file! Then read it when it's time to sleep, and use whatever value you find.

Statistics: Posted by ame — Fri Jan 24, 2025 9:20 pm



Viewing all articles
Browse latest Browse all 5013

Trending Articles