klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

hold "input" for only so long

Mon Mar 13, 2017 1:06 am

Hello,
I would like to run a python3 program at the end I want to ask to reboot or not. If I am not around I would like it to reboot.

Code: Select all


RB = input('Reboot (Y) or N')
RB = RB.upper()
RB = RB[0]
if RB == "N":
    exit()
    pass
else:
    system('sudo reboot')
    exit()
    pass

It stops at RB = input('-->').
is there some other command that will wait for an input for 30 sec?
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

asandford
Posts: 1998
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: hold "input" for only so long

Mon Mar 13, 2017 1:36 am

BASIC has a command called INKEY$ which scans the keyboard rather than waiting for enter to be pressed. If python has (or you can find) an equivalent command, use it in a loop that is controlled by elapsed time. Something like:

Code: Select all

duration=time(now) + 30 seconds
Print 'Reboot (Y) or N'
repeat
reply=inkey$()
until (reply<>"" or time(now)>=duration)
if (reply<>"N" or reply<>"n") then reboot
That will scan for up to 30 seconds or until a key is pressed.

If any other key than N/n is pressed then it will reboot.

ktb
Posts: 1447
Joined: Fri Dec 26, 2014 7:53 pm

Re: hold "input" for only so long

Mon Mar 13, 2017 3:16 am

Maybe threading.Timer could be used.

User avatar
Burngate
Posts: 6303
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: hold "input" for only so long

Mon Mar 13, 2017 4:14 pm

I'm not an expert pythonist but isn't there a way to import a time function, then set up something like

Code: Select all

Import time
Timenow = time
...
If time > Timenow + 30
       ....
This is obviously not going to work as-is, but you get the idea ...

klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Re: hold "input" for only so long

Mon Mar 13, 2017 5:22 pm

Threading worked.
thanks

Code: Select all


#!/usr/bin/python3

from os import system
from threading import Timer


def hello():
    print("hello, world")
    system('sud reboot')


t = Timer(30.0, hello)

try:
    t.start()
except:
    print('did not work')

RB = input('Reboot (Y) or N')
if RB == "":
    RB = 'Y'
RB = RB.upper()
RB = RB[0]
if RB == "N":
    t.cancel()
    exit()
    pass
else:
    t.cancel()
    system('sud reboot')
    exit()
    pass
yes so I could test the sud was meant to be, so I was not rebooting until I got it right.
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

Return to “Python”