Issue
I have an ajax function that is posing data in intervals of 25 seconds to the class that you see bellow
class StateInfo:
flag = None
def post(self):
data = request.get_data()
info = json.loads(data)
Now what I wanna achieve is to set flag variable to 0 when there is no post request within 30 seconds of each other. I know that there is .elapsed but it returns time delta between request and response
Solution
As I already said in the comments, my naive approach would be to add a timestamp for the last post request made. Than simply check if the timestamp is older than 30 seconds.
from multiprocessing import Process
from datetime import timedelta, datetime
class StateInfo:
flag = None
ts = None
def post(self):
ts = datetime.now()
data = request.get_data()
info = json.loads(data)
def check_state(self):
if ts < datetime.now() + timedelta(seconds = -30):
flag = 0
if __name__ == "__main__":
state_info = StateInfo()
proc = Process(target=state_info.check_state())
proc.start()
Note that I've added multiprocessing since you may have a second process, which needs to run at the same time. Although, you may have to repeat the process.
I hope this gives you a good idea, how you can achieve this. Also take a look at the suggestion from @match in the comments.
Answered By - Maik Hasler
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.