Python: Terminate Loop Using Timer - python

I'm quite new on python and working on a school project with this logic: Users have to answer a series of questions as fast as they can, within the given time.
For instance, the time allotted is 30 seconds, I wood loop through a dictionary of questions and get the answer. On timeout, the loop will start, even if the script is still waiting for an input.
def start_test():
for item on questions:
print(item)
answers.append(input(' : '))
I've tried using multiprocessing and multithreading, but I found out that stdin doesn't work subprocesses.
I'm looking for something like:
while duration > 0:
start_test()
def countdown():
global duration
while duration > 0:
duration -= 1
time.sleep(1)
# something lime start_test().stop()
But I can't figure out how to run the countdown function in parallel with the start_test function.
Any ideas?

So as far as I know the input is accessible via main thread only. I might be wrong.
However if that is the case, you need a non-blocking input.
Check this blog. The answer below is based on that.
Note: This is a really quick and dirty solution.
I have checked this on Linux.
If it doesn't work on Windows try this
link for further reference.
import _thread
import sys
import select
import time
def start_test():
questions = ['1','2','3']
answers = []
for item in questions:
print(item)
# Input in a non-blocking way
loop_flag = True
while loop_flag:
# Read documenation and examples on select
ready = select.select([sys.stdin], [], [], 0)[0]
if not ready:
# Check if timer has expired
if timeout:
return answers
else:
for file in ready:
line = file.readline()
if not line: # EOF, input is closed
loop_flag = False
break
elif line.rstrip():
# We have some input
answers.append(line)
# So as to get out of while
loop_flag = False
# Breaking out of for
break
return answers
def countdown():
global timeout
time.sleep(30)
timeout = True
# Global Timeout Flag
timeout = False
timer = _thread.start_new_thread(countdown, ())
answers = start_test()
print(answers)

Related

General way to interrupt scrolling text in console interface

i am trying to make a game in the console and want to have scrolling text. i want to be able to hit a key/type enter and skip the scrolling and print the rest.
so far i tried using pygame (out of the picture due to having to have a display surface active), asyncio with sys.stdin.read(1)(blocked the run on cmd and didnt query user in async based ide's).
this was my latest attempts at this.
import asyncio,time,sys
global skip
immutablesleep = 0.04
mutablesleep = [immutablesleep]
async def aprintl(string,sep="",end="\n",sleep=mutablesleep):
global skip
for letter in string+end:
print(letter+sep,end="",flush=True)
await asyncio.sleep(sleep[0])
skip = True
async def break_print():
global skip
while not skip:
ch = sys.stdin.read(1)
if len(ch)>0:
mutablesleep[0]=0
skip = True
await asyncio.sleep(0.1)
def printl(*args):
global skip
skip = False
mutablesleep[0] = immutablesleep
asyncio.gather(aprintl(*args),break_print())
keep in mind when suggesting modules that i want both os system independant code, and something that can be easly hooked into when freezing modules into exe.
Update:
currently this functions fairly well in terms of interrupting the slow print, but two issues persist:
1:
the interruption by pressing enter is cutting through the printed line, making it unreadable
2:
thread is still waiting for enter even after the print finished.
async def break_print():
global skip, ch
thread = Thread(target=t)
thread.start()
thread.join(timeout=0.1)
while not skip:
if len(ch) > 0:
mutablesleep[0]=0
skip = True
ch = ''
await asyncio.sleep(0.1)
def t():
"""Needed to read from stdin async"""
global ch
ch = sys.stdin.readline()
I believe your problem has to do with the last line
asyncio.gather(aprintl(*args),break_print())
Looking at the docs, the function signature looks like this: awaitable asyncio.gather(*aws, loop=None, return_exceptions=False). The .gather call is likely not working as expected because you are not passing a list of callables, you're instead passing aprintl(*args) to *aws and break_print() is being passed to the loop argument
Change the line to the below, and see if it works as you're expecting.
asyncio.gather([aprintl(*args),break_print()])
Update
I got your code to work, with some caveats
import asyncio
import sys
from threading import Thread
global skip
ch = ''
immutablesleep = 0.04
mutablesleep = [immutablesleep]
async def aprintl(string,sep="",end="\n",sleep=mutablesleep):
global skip
for letter in string+[end]:
if not skip:
print(letter+sep,end="",flush=True)
await asyncio.sleep(sleep[0])
skip = True
async def break_print():
global skip, ch
while not skip:
thread = Thread(target=t)
thread.start()
thread.join(timeout=.1)
if len(ch) > 0:
mutablesleep[0]=0
skip = True
ch = ''
await asyncio.sleep(0.1)
def t():
"""Needed to read from stdin async"""
global ch
ch = sys.stdin.readline()
async def printl(*args):
global skip
skip = False
mutablesleep[0] = immutablesleep
await asyncio.gather(aprintl(*args), break_print())
if __name__ == '__main__':
x = ['asdf ', 'asdf']*5000
asyncio.run(printl(x))
What was changed
Added t() which runs in a Thread for .1 seconds every time break_print runs -- this was required as I believe the reason your initial code isn't running is because it's hanging at the sys.stdin.read(1) line
Run printl() via asyncio.run()
Added an if not skip: check in aprintl(), otherwise it will print the entire input once skipped
Caveats
You must hit enter to stop the printout -- even with .read() you must hit enter. I use readline() because it will return any characters input before the enter key is hit (meaning, you could check to make sure the user input some character before hitting enter: len(ch.strip() > 0): do ...
The script doesn't exit even after skip == True -- this is likely because break_print() doesn't exit when skip == True, it will simply continue to loop.
I realize this may not work in your use case, but I hope it at least gives you some ideas.

Best way to implment timeout for a while loop

I have a while loop
while somecondition:
dostuff
(Sorry. It is hard to give an executable example as this is part of a larger project).
Most of the time the condition is met after a short while and the loop expires. But sometimes the condition will never be met. How can I best catch such cases? Is a timer the best option? How can I implement it best?
You can use a timeout using SIGALRM. Here's a little program that demonstrates it.
import sys
import time
import signal
class TimeoutError(Exception):
pass
def _sig_alarm(sig, tb):
raise TimeoutError("timeout")
def main(argv):
timeout = 7
signal.signal(signal.SIGALRM, _sig_alarm)
try:
signal.alarm(timeout)
while True:
print("sleeping...")
time.sleep(1)
except TimeoutError:
pass
print("Out of loop.")
main(sys.argv)
This sets up a signal handler that simply raises a custom exception (but you can use any), and then catches it.
wait for an hour example
from datetime import timedelta
delete_TO = 1
wait_until = datetime.now() + timedelta(hours=delete_TO)
break_loop = False
while not break_loop:
do-your loop-stuff
if wait_until < datetime.now() or somecondition:
break_loop = True
(edited: wait_until must be smaller than datetime.now() )
The correct answer is
from datetime import timedelta
delete_TO = 1
wait_until = datetime.now() + timedelta(hours=delete_TO)
break_loop = False
while not break_loop:
do-your loop-stuff
if wait_until < datetime.now() or somecondition:
break_loop = True
This code is copy pasted from #ohad_the_lad (https://stackoverflow.com/users/2468201/ohad-the-lad) answer, but it fixes the unintentional bug where the loop exits if wait_until is greater than datetime.now() which will always happen.
How about writing a for loop? Or you can implement something like timer=0 an increment it every time you pass the loop. For example if you wanted to break after 100 iterations you could write an if statement. If timer == 100 then break.

How to structure code to be able to launch tasks that can kill/replace each other

I have a Python program that does the following:
1) endlessly wait on com port a command character
2) on character reception, launch a new thread to execute a particular piece of code
What I would need to do if a new command is received is:
1) kill the previous thread
2) launch a new one
I read here and there that doing so is not the right way to proceed.
What would be the best way to do this knowing that I need to do this in the same process so I guess I need to use threads ...
I would suggest you two differente approaches:
if your processes are both called internally from a function, you could set a timeout on the first function.
if you are running external script, you might want to kill the process.
Let me try to be more precise in my question by adding an example of my code structure.
Suppose synchronous functionA is still running because waiting internally for a particular event, if command "c" is received, I need to stop functionA and launch functionC.
def functionA():
....
....
call a synchronous serviceA that can take several seconds even more to execute
....
....
def functionB():
....
....
call a synchronous serviceB that nearly returns immediately
....
....
def functionC():
....
....
call a synchronous serviceC
....
....
#-------------------
def launch_async_task(function):
t = threading.Thread(target=function, name="async")
t.setDaemon(True)
t.start()
#------main----------
while True:
try:
car = COM_port.read(1)
if car == "a":
launch_async_task(functionA)
elif car == "b":
launch_async_task(functionB)
elif car == "c":
launch_async_task(functionC)
May want to run the serial port in a separate thread. When it receives a byte put that byte in a queue. Have the main program loop and check the queue to decide what to do with it. From the main program you can kill the thread with join and start a new thread. You may also want to look into a thread pool to see if it is what you want.
ser = serial.Serial("COM1", 9600)
que = queue.Queue()
def read_serial(com, q):
val = com.read(1)
q.put(val)
ser_th = threading.Thread(target=read_serial, args=(ser, que))
ser_th.start()
th = None
while True:
if not que.empty():
val = que.get()
if val == b"e":
break # quit
elif val == b"a":
if th is not None:
th.join(0) # Kill the previous function
th = threading.Thread(target=functionA)
th.start()
elif val == b"b":
if th is not None:
th.join(0) # Kill the previous function
th = threading.Thread(target=functionB)
th.start()
elif val == b"c":
if th is not None:
th.join(0) # Kill the previous thread (functionA)
th = threading.Thread(target=functionC)
th.start()
try:
ser.close()
th.join(0)
except:
pass
If you are creating and joining a lot of threads you may want to just have a function that checks what command to run.
running = True
def run_options(option):
if option == 0:
print("Running Option 0")
elif option == 1:
print("Running Option 1")
else:
running = False
while running:
if not que.empty():
val = que.get()
run_options(val)
Ok, I finally used a piece of code that uses ctypes lib to provide some kind of killing thread function.
I know this is not a clean way to proceed but in my case, there are no resources shared by the threads so it shouldn't have any impact ...
If it can help, here is the piece of code that can easily be found on the net:
def terminate_thread(thread):
"""Terminates a python thread from another thread.
:param thread: a threading.Thread instance
"""
if not thread.isAlive():
return
exc = ctypes.py_object(SystemExit)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(
ctypes.c_long(thread.ident), exc)
if res == 0:
raise ValueError("nonexistent thread id")
elif res > 1:
# """if it returns a number greater than one, you're in trouble,
# and you should call it again with exc=NULL to revert the effect"""
ctypes.pythonapi.PyThreadState_SetAsyncExc(thread.ident, None)
raise SystemError("PyThreadState_SetAsyncExc failed")

wxPython: Stop threading with wxButton

How I can stop threading by clicking a wxButton?
Here is my code:
def startMonitor(self,event):
selectedInterface = self.interfaces_cblist.GetValue()
Publisher().sendMessage(("test"),selectedInterface)
self.Close()
selectInterfaceStr = str(selectedInterface)
if len(selectedInterface) == 0:
noSelect_error = wx.MessageDialog(None,"Please select an interface","",wx.OK|wx.ICON_ERROR)
noSelect_error.ShowModal()
else:
monitorStarted = wx.MessageDialog(None,"Monitor on %s started"%selectInterfaceStr,"",wx.OK|wx.ICON_ERROR)
monitorStarted.ShowModal()
self.monitorInterface_button.Disable()
threading.Thread(target=self.camtableDetection,args=(selectInterfaceStr,)).start()
threading.Thread(target=self.dhcpexhaustion,args=(selectInterfaceStr,)).start()
def camtableDetection(self,getInterface):
global interface
interface = str(getInterface)
THRESH=(254/4)
START = 5
def monitorPackets(p):
if p.haslayer(IP):
hwSrc = p.getlayer(Ether).src
if hwSrc not in hwList:
hwList.append(hwSrc)
delta = datetime.datetime.now() - start
if((delta.seconds > START) and ((len(hwList)/delta.seconds) > THRESH)):
print "[*]- Detected CAM Table Attack."
#camAttackDetected = wx.MessageDialog(None,"Cam Attack Detected","",wx.ICON_ERROR)
#camAttackDetected.ShowModal()
hwList = []
start = datetime.datetime.now()
sniff(iface=interface,prn=monitorPackets)
def dhcpexhaustion(self,getInterface):
interface = str(getInterface)
global reqCnt
global ofrCnt
reqCnt = 0
ofrCnt = 0
def monitorPackets(p):
if p.haslayer(BOOTP):
global reqCnt
global ofrCnt
opCode = p.getlayer(BOOTP).op
if opCode == 1:
reqCnt=reqCnt+1
elif opCode == 2:
ofrCnt=ofrCnt+1
print "[*] - "+str(reqCnt)+" Requests, "+str(ofrCnt)+" Offers."
sniff(iface=interface,prn=monitorPackets)
I am thinking to stop the threading when I click on a button, but have no idea how can it can be done.
There are self.abort techniques, but I'm not sure how to apply it in my code.
As I said in a comment:
If [sniff is] a function that you have no control over (e.g., from a C extension module) and it loops forever, then it must have some way to cancel it. Maybe it's having your callback return a special value, maybe it's calling a control function, maybe it's closing the object it's working on… whatever it is, you have to do that.
So, why not read the documentation for scapy.sniff to see how to cancel it?
Sniff packets
sniff([count=0,] [prn=None,] [store=1,] [offline=None,] [lfilter=None,] + L2ListenSocket args) -> list of packets
count: number of packets to capture. 0 means infinity
store: wether to store sniffed packets or discard them
prn: function to apply to each packet. If something is returned,
it is displayed. Ex:
ex: prn = lambda x: x.summary()
lfilter: python function applied to each packet to determine
if further action may be done
ex: lfilter = lambda x: x.haslayer(Padding)
offline: pcap file to read packets from, instead of sniffing them
timeout: stop sniffing after a given time (default: None)
L2socket: use the provided L2socket
opened_socket: provide an object ready to use .recv() on
stop_filter: python function applied to each packet to determine
if we have to stop the capture after this packet
ex: stop_filter = lambda x: x.haslayer(TCP)
So, the way to stop it sniffing forever is to pass it a stop_filter function, which will return True when you want to stop it. So that function is where you're going to check your stop flag. For example:
def __init__(self, whatever):
self.stopflag = False
self.stoplock = threading.Lock()
# rest of your init
def stop(self):
with self.stoplock:
self.stopflag = True
def stop_filter(self):
with self.stoplock:
return self.stopflag
def dhcpexhaustion(self, getInterface):
# etc.
sniff(iface=interface,prn=monitorPackets, stop_filter=self.stop_filter)
You're probably going to want to store the two Thread objects at start time, so you can join them at stop time, rather than just leaking them until your program exits. But otherwise, this should do it.

How to end program running after given time in Python

I'd like my Python program to run an algorithm for a given number of seconds and then to print the best result so far and to end.
What is the best way to do so?
I tried the following but it did not work(the program kept running after the printing):
def printBestResult(self):
print(self.bestResult)
sys.exit()
def findBestResult(self,time):
self.t = threading.Timer(time, self.printBestResult)
self.t.start()
while(1):
# find best result
Untested code, but something like this?
import time
threshold = 60
start = time.time()
best_run = threshold
while time.time()-start < threshold:
run_start = time.time()
doSomething()
run_time = time.time() - start
if run_time < best_run:
best_run = run_time
On unix, you can use signals -- This code times out after 1 second and counts how many times it iterates through the while loop in that time:
import signal
import sys
def handle_alarm(args):
print args.best_val
sys.exit()
class Foo(object):
pass
self=Foo() #some mutable object to mess with in the loop
self.best_val=0
signal.signal(signal.SIGALRM,lambda *args: handle_alarm(self))
signal.alarm(1) #timeout after 1 second
while True:
self.best_val+=1 # do something to mutate "self" here.
Or, you could easily have your alarm_handler raise an exception which you then catch outside the while loop, printing your best result.
If you want to do this with threads, a good way is to use an Event. Note that signal.alarm won't work in Windows, so I think threading is your best bet unless in that case.
import threading
import time
import random
class StochasticSearch(object):
def __init__(self):
self.halt_event = threading.Event()
def find_best_result(self, duration):
halt_thread = threading.Timer(duration, self.halt_event.set)
halt_thread.start()
best_result = 0
while not self.halt_event.is_set():
result = self.search()
best_result = result if result > best_result else best_result
time.sleep(0.5)
return best_result
def search(self):
val = random.randrange(0, 10000)
print 'searching for something; found {}'.format(val)
return val
print StochasticSearch().find_best_result(3)
You need an exit condition, or the program will run forever (or until it runs out of memory). Add one yourself.

Categories