Python中语句的执行可以延迟吗?

发布于 2024-09-11 10:45:58 字数 207 浏览 7 评论 0原文

我希望它运行第一行 print 1 然后等待 1 秒运行第二个命令 print 2 等。

伪代码:

print 1
wait(1 seconds)
print 2
wait(0.45 seconds)
print 3
wait(3 seconds)
print 4

I want it to run the first line print 1 then wait 1 second to run the second command print 2, etc.

Pseudo-code:

print 1
wait(1 seconds)
print 2
wait(0.45 seconds)
print 3
wait(3 seconds)
print 4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白昼 2024-09-18 10:45:58

time.sleep(秒)

import time

print 1
time.sleep(1)
print 2
time.sleep(0.45)
print 3
time.sleep(3)
print 4

time.sleep(seconds)

import time

print 1
time.sleep(1)
print 2
time.sleep(0.45)
print 3
time.sleep(3)
print 4
牛↙奶布丁 2024-09-18 10:45:58

所有答案都假设您想要或可以在每行之后手动插入 time.sleep ,但您可能想要一种自动方式来对大量代码行执行此操作,例如考虑此代码

def func1():
    print "func1 1",time.time()
    print "func1 2",time.time()

def func2():
    print "func2 1",time.time()
    print "func2 2",time.time()

def main():
    print 1,time.time()
    print 2,time.time()
    func1()
    func2()

如果如果你想延迟每行的执行,要么你可以在每行之前手动插入 time.sleep ,这样既麻烦又容易出错,也可以使用 sys.settrace 来在执行每行之前调用您自己的函数,并且在该回调中您可以延迟执行,因此无需在每个地方手动插入 time.sleep 并乱扔代码,您可以这样做而不

import sys
import time

def func1():
    print "func1 1",time.time()
    print "func1 2",time.time()

def func2():
    print "func2 1",time.time()
    print "func2 2",time.time()

def main():
    print 1,time.time()
    print 2,time.time()
    func1()
    func2()

def mytrace(frame, event, arg):
    if event == "line":
        time.sleep(1)
    return mytrace

sys.settrace(mytrace)
main()

需要跟踪输出:

1 1280032100.88
2 1280032100.88
func1 1 1280032100.88
func1 2 1280032100.88
func2 1 1280032100.88
func2 2 1280032100.88

跟踪输出是:

1 1280032131.27
2 1280032132.27
func1 1 1280032134.27
func1 2 1280032135.27
func2 1 1280032137.27
func2 2 1280032138.27

您可以根据需要进一步调整它,也可以检查行内容,最重要的是,这很容易禁用并且适用于任何代码。

All the answers have assumed that you want or can manually insert time.sleep after each line, but may be you want a automated way to do that for a large number of lines of code e.g. consider this code

def func1():
    print "func1 1",time.time()
    print "func1 2",time.time()

def func2():
    print "func2 1",time.time()
    print "func2 2",time.time()

def main():
    print 1,time.time()
    print 2,time.time()
    func1()
    func2()

If you want to delay execution of each line, either you can manually insert time.sleep before each line which is cumbersome and error-prone, instead you can use sys.settrace to get you own function called before each line is executed and in that callback you can delay execution, so without manually inserting time.sleep at every place and littering code, you can do this instead

import sys
import time

def func1():
    print "func1 1",time.time()
    print "func1 2",time.time()

def func2():
    print "func2 1",time.time()
    print "func2 2",time.time()

def main():
    print 1,time.time()
    print 2,time.time()
    func1()
    func2()

def mytrace(frame, event, arg):
    if event == "line":
        time.sleep(1)
    return mytrace

sys.settrace(mytrace)
main()

Without trace output is:

1 1280032100.88
2 1280032100.88
func1 1 1280032100.88
func1 2 1280032100.88
func2 1 1280032100.88
func2 2 1280032100.88

With trace output is:

1 1280032131.27
2 1280032132.27
func1 1 1280032134.27
func1 2 1280032135.27
func2 1 1280032137.27
func2 2 1280032138.27

You can further tweak it according to your needs, may be checking line contents too and most importantly this is very easy to disable and will work with any code.

2024-09-18 10:45:58
import time

# ...

time.sleep(1)
import time

# ...

time.sleep(1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文