-1

I want to set an initial time in zero (0 seconds) and do something until the end of run time duration (let's say 3600 seconds)

This program is for simulation and I think I can't simply use counter since the other procedures will occur while the simulation is running.

For example

start = 0
run_time = 3600
timer = 0
while timer <= run_time:
    print(timer)
    # do other stuff
AGH
  • 300
  • 3
  • 12
  • check out the 2nd answer [here](https://stackoverflow.com/questions/55287184/run-python-program-until-specific-time) and this [thread](https://stackoverflow.com/questions/1191374/using-module-subprocess-with-timeout) – Gene Burinsky Jul 09 '20 at 01:29

3 Answers3

0

Try this:

from time import time

print("Stopwatch running..")
t0 = time()
watch = 0

while watch-t0 < 3:
    watch = time()
    # Your Code

print(" Finished!\n Time =",watch - t0)
user10637953
  • 370
  • 1
  • 10
0

You could simply use datetime module:

import datetime as dt

start = dt.datetime.now()
run_time = dt.timedelta(seconds=3600)

while dt.datetime.now() <= start + run_time:
   ....
   ....
NotAName
  • 3,821
  • 2
  • 29
  • 44
0

I see what you mean, you can use threads to accomplish this

from threading import Thread
from time import sleep

run_time = 10 #seconds

def WhateverYourDoing():
    while run:
        print("Look, I'm doing stuff!")
        sleep(1)

sim = Thread(target=WhateverYourDoing)

for x in range(3, 0, -1):
    print(x)
    sleep(1)
print("Go!")
run = True
sim.start()
sleep(run_time)
run = False
print("DONE")
MichaelT572
  • 178
  • 1
  • 9