每隔X分钟运行一次函数 - Python

时间:2021-09-08 05:42:58

I'm using Python and PyGTK. I'm interested in running a certain function, which gets data from a serial port and saves it, every several minutes.

我正在使用Python和PyGTK。我对运行某个函数感兴趣,该函数每隔几分钟从串口获取数据并保存。

Currently, I'm using the sleep() function in the time library. In order to be able to do processing, I have my system set up like this:

目前,我在时间库中使用sleep()函数。为了能够进行处理,我的系统设置如下:

import time
waittime = 300 # 5 minutes
while(1):
    time1 = time.time()
    readserial() # Read data from serial port
    processing() # Do stuff with serial data, including dumping it to a file
    time2 = time.time()
    processingtime = time2 - time1
    sleeptime = waittime - processingtime
    time.sleep(sleeptime)

This setup allows me to have 5 minute intervals between reading data from the serial port. My issue is that I'd like to be able to have my readserial() function pause whatever is going on every 5 minutes and be able to do things all the time instead of using the time.sleep() function.

此设置允许我在从串行端口读取数据之间有5分钟的间隔。我的问题是,我希望能够让我的readserial()函数暂停每5分钟发生一次,并且能够一直做事而不是使用time.sleep()函数。

Any suggestions on how to solve this problem? Multithreading? Interrupts? Please keep in mind that I'm using python.

有关如何解决这个问题的任何建议?多线程?中断?请记住我正在使用python。

Thanks.

谢谢。

4 个解决方案

#1


23  

Do not use such loop with sleep, it will block gtk from processing any UI events, instead use gtk timer e.g.

不要在睡眠时使用这样的循环,它会阻止gtk处理任何UI事件,而是使用gtk计时器,例如

def my_timer(*args):
    return True# do ur work here, but not for long

gtk.timeout_add(60*1000, my_timer) # call every min

#2


10  

This is exactly like my answer here

这和我的回答完全一样

If the time is not critical to be exact to the tenth of a second, use

如果时间对于精确到十分之一秒并不重要,请使用

glib.timeout_add_seconds(60, ..)

else as above.

如上所述。

timeout_add_seconds allows the system to align timeouts to other events, in the long run reducing CPU wakeups (especially if the timeout is reocurring) and save energy for the planet(!)

timeout_add_seconds允许系统将超时与其他事件对齐,从长远来看减少CPU唤醒(特别是如果超时重新启动)并为行星节省能量(!)

#3


4  

gtk.timeout_add appears to be deprecated, so you should use

gtk.timeout_add似乎已被弃用,因此您应该使用

def my_timer(*args):
    # Do your work here
    return True

gobject.timeout_add( 60*1000, my_timer )

#4


1  

try:

尝试:

import wx
wx.CallLater(1000, my_timer)

#1


23  

Do not use such loop with sleep, it will block gtk from processing any UI events, instead use gtk timer e.g.

不要在睡眠时使用这样的循环,它会阻止gtk处理任何UI事件,而是使用gtk计时器,例如

def my_timer(*args):
    return True# do ur work here, but not for long

gtk.timeout_add(60*1000, my_timer) # call every min

#2


10  

This is exactly like my answer here

这和我的回答完全一样

If the time is not critical to be exact to the tenth of a second, use

如果时间对于精确到十分之一秒并不重要,请使用

glib.timeout_add_seconds(60, ..)

else as above.

如上所述。

timeout_add_seconds allows the system to align timeouts to other events, in the long run reducing CPU wakeups (especially if the timeout is reocurring) and save energy for the planet(!)

timeout_add_seconds允许系统将超时与其他事件对齐,从长远来看减少CPU唤醒(特别是如果超时重新启动)并为行星节省能量(!)

#3


4  

gtk.timeout_add appears to be deprecated, so you should use

gtk.timeout_add似乎已被弃用,因此您应该使用

def my_timer(*args):
    # Do your work here
    return True

gobject.timeout_add( 60*1000, my_timer )

#4


1  

try:

尝试:

import wx
wx.CallLater(1000, my_timer)