本文目录一览:
- python time.sleep 随机数
- python每隔10秒运行一个指定函数怎么实现
- python每隔N秒运行指定函数的方法
- 我想写一个python让其纸上面在随机时间20秒到100秒之间打印一遍hello,world?
python time.sleep 随机数
你可以查看一下帮助。比如这样子
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
help(time.sleep)
Help on built-in function sleep in module time:
sleep(...)
sleep(seconds)
Delay execution for a given number of seconds. The argument may be
a floating point number for subsecond precision.
time.sleep(5,30)
Traceback (most recent call last):
File "stdin", line 1, in module
TypeError: sleep() takes exactly 1 argument (2 given)
从帮助里可以看到。sleep(这里里允许时间秒)秒可以是浮点数。但是不允许多个参数。
如果想随机可以使用random
比如
import random,time
time.sleep(random.randint(5,30))
python每隔10秒运行一个指定函数怎么实现
# 脚本里面直接这样写就好了
import random
def R():
print(random.randint(1,1000))
for i in range(1,10):
R()
python每隔N秒运行指定函数的方法
python每隔N秒运行指定函数的方法 这篇文章主要介绍了python每隔N秒运行指定函数的方法,涉及Python的线程与时间操作技巧,非常具有实用价值,需要的朋友可以参考下 这是一个类似定时器的效果,每隔指定的秒数运行指定的函数,采用线程实现,代码简单实用。 代码如下:
import os
import time
def print_ts(message):
print("[%s] %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), message))
def run(interval, command):
print_ts("-" * 100)
print_ts("Command %s" % command)
print_ts("Starting every %s seconds." % interval)
print_ts("-" * 100)
while True:
try:
# sleep for the remaining seconds of interval
time_remaining = interval - time.time() % interval
print_ts("Sleeping until %s (%s seconds)..." % ((time.ctime(time.time() + time_remaining)), time_remaining))
time.sleep(time_remaining)
print_ts("Starting command.")
# execute the command
status = os.system(command)
print_ts("-" * 100)
print_ts("Command status = %s." % status)
except Exception as e:
print(e)
if __name__ == "__main__":
interval = 5
command = r"ipconfig"
run(interval, command)
希望本文所述对大家的Python程序设计有所帮助。
我想写一个python让其纸上面在随机时间20秒到100秒之间打印一遍hello,world?
import time
import random
while True: # 循环条件,可以自行修改,这里设置一直运行下去
sleeptime = random.randint(20, 100) # 在这里产生20到100之间的随机整数
time.sleep(sleeptime) # 根据上步产生的时间进行休眠
print("hello,world") # 输出hello,world