每 x 秒执行一次函数 python [英] execute function every x number of secods python

查看:67
本文介绍了每 x 秒执行一次函数 python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找解决方案时遇到了这个线程,但它并没有完全满足我的需要:

I came across this thread when I was looking for a solution, but it doesn't quite do what I need it to:

在 Python 中每 x 秒重复执行一个函数的最佳方法是什么?

这实际上有效"(或至少第一个解决方案有效),但它不允许您与脚本的其余部分同时进行.

This actually "works" (or at least the first solution does) but it doesn't allow you to do it simultaneously along with the rest of the script.

基本上,我需要执行这样的函数:

Basically, I need to execute a function like this:

    def functionName():
        print "text"

我需要它每隔 100 毫秒执行一次.但我需要这个 while 循环同时循环:

I need this to execute every, say, 100 milliseconds. But I need this while loop to be looping simultaneously:

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

我该怎么做?

谢谢!

推荐答案

不应该这样工作:http://www.pygame.org/docs/ref/time.html#pygame.time.set_timer

pygame.init()
pygame.time.set_timer(USEREVENT + 1, 100)
while True:
    for event in pygame.event.get():
        if event.type == USEREVENT + 1:
           functionName()
        if event.type == QUIT:
            pygame.quite()
            sys.exit()

这篇关于每 x 秒执行一次函数 python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆