I want to simulate time in Python. I want to apply “my_fct” every second, however I do not know how long “my_fct” takes to run so I cannot use a sleep.
What I did is this:
past_time = datetime.datetime.utcnow()
present_time = datetime.datetime.utcnow()
for i in range(10):
while( (present_time - past_time).total_seconds() < 1):
present_time = datetime.datetime.utcnow()
my_fct(......)
past_time = present_time
I don’t think it is the good way to do it? What is the right solution?
Thank you
running your code gives:
what you need is this:
time-time gives a timedelta value. u need the seconds of this value. and then compare to your int.
edit: however, i now realize this is not the solution you are looking for.
this is a way to do it with threads (a very simple way – prone to problems depending on what
my_fct()does.)