Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用于模拟的秒表计时器_Python_Time_Timer_Simulation_Stopwatch - Fatal编程技术网

Python 用于模拟的秒表计时器

Python 用于模拟的秒表计时器,python,time,timer,simulation,stopwatch,Python,Time,Timer,Simulation,Stopwatch,我想将初始时间设置为零(0秒),并在运行时持续时间结束之前执行一些操作(比如3600秒) 这个程序是用于模拟的,我想我不能简单地使用计数器,因为其他程序会在模拟运行时发生 比如说 start = 0 run_time = 3600 timer = 0 while timer <= run_time: print(timer) # do other stuff start=0 运行时间=3600 计时器=0 当定时器时,尝试以下操作: from time import ti

我想将初始时间设置为零(0秒),并在运行时持续时间结束之前执行一些操作(比如3600秒)

这个程序是用于模拟的,我想我不能简单地使用计数器,因为其他程序会在模拟运行时发生

比如说

start = 0
run_time = 3600
timer = 0
while timer <= run_time:
    print(timer)
    # do other stuff
start=0
运行时间=3600
计时器=0
当定时器时,尝试以下操作:

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)
从时间导入时间
打印(“秒表运行…”)
t0=时间()
手表=0
当watch-t0<3时:
手表=时间
#你的代码
打印(“已完成!\n Time=,监视-t0)

您可以简单地使用
datetime
模块:

import datetime as dt

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

while dt.datetime.now() <= start + run_time:
   ....
   ....
将日期时间导入为dt
start=dt.datetime.now()
运行时间=dt.timedelta(秒=3600)

虽然dt.datetime.now()我明白你的意思,但你可以使用线程来完成这项任务

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")
看看第二个答案和这个