I'm writing a python game-bot where a user needs to send in an input within a certain amount of time.
My current implementation of this is to spawn a new thread that sleeps for that amount of time, then checks if an input has been sent. A nonce is added as the time could run out after another thread starts which would cause the if chatid in user_threads part to evaluate to true. Should I use this method or is there a better solution to having a timed input (in the sense less likely to fail due to like race conditions).
I've also tried using multiprocessing.Process but sharing variables to the other process seems to be a lot more cumbersome.
from threading import Thread
from time import time, sleep
from random import randint
user_threads = {}
def timeout(t,r):
while time()-t<5:
sleep(5-time()+t)
if chatid in user_threads:
if user_threads[chatid][3] != r:
return
del user_threads[chatid]
print("Too slow")
def recvans(ans,inp):
if ans==inp:
print("yes")
else:
print("no")
def starttimer(chatid):
r = randint(0,1<<128)
user_threads[chatid] = [None,None,None,r]
user_threads[chatid][2] = ["a"]
P = Thread(target = timeout, args = (time(),r))
user_threads[chatid][0] = P
user_threads[chatid][1] = recvans
P.start()
while True: # simulating user input from different users (here chatid=1)
inp = input()
chatid = 1
if chatid in user_threads:
t, func, args, _ = user_threads[chatid]
if t == None:
print("Please wait")
continue
del user_threads[chatid]
args += [inp]
func(*args)
continue
if inp == "Start":
starttimer(chatid)
continue
if inp == "Quit":
break
print("Unknown msg")