如何使用threading.Timer创建触发器? [英] How to create a trigger with threading.Timer?

查看:85
本文介绍了如何使用threading.Timer创建触发器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上个月才发现python,我根本不是程序员.我正在尝试为电子仪器创建自己的软件. 我实际上需要创建一个触发器,每隔0.04毫秒调用一次函数 (如果它是可能的).我尝试过:

I just discover python last month, I'm not a programmer at all. I'm trying to create my own software for an electronic instrument. I actually need to create a trigger which will call a function each 0.04 ms (if it's possible). I tried with this:

在我的第一个文件Metronome.py中:

In my first file Metronome.py:

class Metronome:

    def __init__(self, clip, _matrix):
        self.clip = clip
        self._matrix = _matrix
        self.inc = -1


    def _trigger(self): 
        self.inc = self.inc + 1
        self._matrix.get_button(self.inc, 1).send_value(GREEN_FULL)
        t = threading.Timer(1.0, self._trigger).start()

在第二个文件和一个新类中:

In a second file and a new class:

我使用以下命令导入以前的功能:

I import the previous function with:

from Metronome.py import Metronome

我使用以下命令调用上一个函数:

I call the previous function with:

Metronome(self.clip, self._matrix)._trigger()

_trigger功能中,self._matrix.get_button(self.inc, 1).send_value(GREEN_FULL)允许我在仪器界面上发送LED反馈. 当我启动该程序时,实际上是在仪器上获得了第一份LED反馈.但是,其他设置要花费1秒钟以上的时间,其中一些会同时出现.同样,在每次反馈之后,下一个反馈的时间会增加(例如5/10秒).我不明白!

In the _trigger function, self._matrix.get_button(self.inc, 1).send_value(GREEN_FULL) allows me to send a led feedback on my instrument interface. When I launch the program, I actually get the first led feedback on my instrument. However, the others take more than 1 second to setup and some of them appear at the same time. Also, after each feedback the time of the next one is increased (like 5/10 seconde). I don't get it!

推荐答案

除了有关如何调用Metronome类的其他问题(请参见上面的注释)之外,我认为_trigger方法应该更像:

apart from other issues on how the Metronome class is called (see comments above), I think the _trigger method should be more like:

def _trigger(self):
    while True:
        self.inc += 1
        self._matrix.get_button(self.inc, 1).send_value(GREEN_FULL)
        if (whatever_exit_condition):
            break
        time.sleep(time_interval_in_seconds)

如果没有退出条件,只需移除if块.

if there's no exit condition, just remove the if block.

简而言之:避免在每个周期上创建一个新的计时器. 相反,当您开始调用该方法时,仅创建一个Timer:

In a few words: avoid creating a new timer on every cycle. Instead, create one Timer only, when you start calling the method:

t = threading.Timer(1.0, Metronome(self.clip, self._matrix)._trigger).start()

以下是有关如何使用计时器的有效示例:

Here is a working example on how to use the timer:

#! /usr/bin/env python
from sys import argv
import time
import threading


def f():
    retry = 0
    while True:
        print "f, retry=", retry
        retry += 1
        if retry > 3:
            break
        time.sleep(2)
    print ("f exited")


if __name__ == "__main__":
    if len(argv) <= 1:
        print "usage: %s num" % (argv[0],)
        exit(1)
    t = threading.Timer(1.0, f)
    t.start()
    for i in range(int(argv[1])):
        time.sleep(1)
        print "main, i=", i

如果使用以下命令运行它:

if you run it with:

python timer.py 10

它输出:

f, retry= 0
main, i= 0
main, i= 1
f, retry= 1
main, i= 2
main, i= 3
f, retry= 2
main, i= 4
main, i= 5
f, retry= 3
f exited
main, i= 6
main, i= 7
main, i= 8
main, i= 9

很容易地说,主线程是活着的,并且做他的事情,而计时器线程是通过计时器创建的,并且它在有限的次数内(例如,向您的用户发信号)继续执行自己的位(3) ),在每个周期之间睡觉. 我希望现在更加清楚.

Basicallly the main thread is alive and does his thing, while the timer thread is created via the timer once and it keeps doing its bit (e.g. signal something to the user in your case), for a limited number of times (3), sleeping between each cycle. I hope it is clearer now.

这篇关于如何使用threading.Timer创建触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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