<p>我在用Python和PyGTK。我对运行一个特定的函数很感兴趣,它每几分钟从串行端口获取数据并保存数据。</p>
<p>目前,我正在使用时间库中的sleep()函数。为了能够进行处理,我的系统设置如下:</p>
<pre><code>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)
</code></pre>
<p>此设置允许我在从串行端口读取数据之间有5分钟的间隔。我的问题是,我想让readserial()函数每5分钟暂停一次正在进行的任何操作,并且能够一直执行操作,而不是使用time.sleep()函数。</p>
<p>对如何解决这个问题有什么建议吗?多线程?中断?请记住我正在使用python。</p>
<p>谢谢。</p>
<p>gtk.timeout_add似乎已弃用,因此应使用</p>
<pre><code>def my_timer(*args):
# Do your work here
return True
gobject.timeout_add( 60*1000, my_timer )
</code></pre>