我正在使用Python和PyGTK.我对运行某个函数感兴趣,该函数每隔几分钟从串口获取数据并保存.
目前,我在时间库中使用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)
Run Code Online (Sandbox Code Playgroud)
此设置允许我在从串行端口读取数据之间有5分钟的间隔.我的问题是,我希望能够让我的readserial()函数暂停每5分钟发生一次,并且能够一直做事而不是使用time.sleep()函数.
有关如何解决这个问题的任何建议?多线程?中断?请记住我正在使用python.
谢谢.
我在我的Matlab项目中使用tic-toc函数的地方很多.输出时间可以是331.5264 or 1234.754 seconds等.我可以输出这是分钟格式吗?例如.5 minutes and 30.6 seconds?谢谢!
是否可以确定Delphi中的TTimer何时触发?我知道如何根据计时器的上次运行和计时器间隔计算出来.不幸的是,我正在使用的代码有很多状态,间隔可以在很多方面改变.我宁愿不必跟踪上次启用计时器和更改间隔的时间,而是直接从计时器访问此信息.
场景:计时器有2分钟的间隔,自上次启用以来已经过了30秒,如何在代码中发现计时器事件将在90秒内再次触发?
是否可以直接从计时器获取此信息?或者OS?计时器组件必须"知道"下一次触发它的时间.怎么知道的?我可以访问这些信息吗?
在支持时间戳计数器(TSC)的处理器中,Linux使用TSC提供高分辨率计时器选项.根据我的理解,TSC是一个可以读取的寄存器,但不提供以配置的速率中断CPU的选项.因此,对于Linux中的定时器中断生成仍然必须依赖I/O APIC(在x86上),HZ值通常设置为1000或250.
即使TSC以微秒粒度给出时间戳,定时器/调度粒度仍将是4ms或1ms,具体取决于HZ值.这种理解是否正确?或者是否可以使用TSC改进计时器粒度?
我刚刚用新的3.11内核安装了Ubuntu 13.10.在3.10中,它具有无滴漏功能,我可以在不受本地定时器中断的情况下运行进程,而不像以前那么多.我关注此链接http://www.breakage.org/2013/11/nohz_fullgodmode/
我计划在cpu 3上运行我的应用程序,所以我在grub中设置以下内容:
isolcpus=3 nohz_full=3 rcu_nocbs=3
Run Code Online (Sandbox Code Playgroud)
重新启动后,似乎cpu 3上的本地定时器中断确实比其他cpu要少很多.
我也跑了:
# for i in `pgrep rcu` ; do taskset -pc 0 $i ; done
Run Code Online (Sandbox Code Playgroud)
但是当我开始运行我的应用程序时,本地计时器中断的计数跳了起来.我的应用程序只做无限循环.
int main() {
while (true) {
}
}
Run Code Online (Sandbox Code Playgroud)
那我错过了什么?当我运行时,为什么时间中断会回来?我认为nohz_full意味着当只有一个进程在运行时,它将停止中断.
以下是/ proc/sched_debug的输出,当我没有运行应用程序时,显然在该cpu上没有其他进程.那我错过了什么?
cpu#3, 2492.071 MHz
.nr_running : 0
.load : 0
.nr_switches : 45818
.nr_load_updates : 11165
.nr_uninterruptible : -1
.next_balance : 4295.674289
.curr->pid : 0
.clock : 3127610.519188
.cpu_load[0] : 0
.cpu_load[1] : 0
.cpu_load[2] : 0
.cpu_load[3] : 0
.cpu_load[4] : 0
.yld_count …Run Code Online (Sandbox Code Playgroud) 我需要编写一个接收事件的组件(事件具有唯一的ID).每个活动都要求我发出请求.该事件指定一个超时期限,等待来自请求的响应.
如果响应在计时器触发之前响起,那很好,我取消了计时器.如果计时器首先触发,那么请求超时,我想继续前进.
此超时时间是在事件中指定的,因此它不是常量.预期的超时时间在30秒到5分钟的范围内.
我可以看到实现这个的两种方法.
选项1似乎是最简单的解决方案,但我担心创建这么多计时器可能不是一个好主意,因为计时器可能太昂贵了.在创建大量计时器时是否存在任何陷阱?我怀疑在后台,定时器实现可能实际上是选项2的有效实现.如果这个选项是个好主意,我应该使用哪个定时器?System.Timers.Timer或System.Threading.Timer.
选项2似乎更多的工作,与选项1相比可能不是一个有效的解决方案.
更新
我期望的最大定时器数量在10000的范围内,但更有可能在100的范围内.另外,正常情况是在发射之前取消定时器.
更新2
我使用10K实例运行测试,System.Threading.Timer并System.Timers.Timer密切关注线程数和内存.System.Threading.Timer与System.Timers.Timer通过内存使用判断相比,似乎"更轻" ,并且没有为两个定时器创建过多的线程(即 - 线程池正常工作).所以我决定继续使用System.Threading.Timer.
最近我看到http://developer.android.com/reference/android/os/CountDownTimer.html,并想知道是否有一个相应的秒表类,因为我想告诉用户我的应用程序他已经尝试解决了多长时间这个谜题.我的意思是自己编程这样的秒表并不复杂.我试过类似的东西
Runnable runnable = new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
long seconds = (System.currentTimeMillis() - t) / 1000;
statusBar.setText(String.format("%02d:%02d", seconds / 60, seconds % 60));
}
}
};
statusBar.post(runnable);
Run Code Online (Sandbox Code Playgroud)
但奇怪的是我的活动的布局不再膨胀,因为我statusBar.post(runnable);在acitivity的onCreate方法结束时有这个意思,这意味着在启动应用程序后我只看到一个白色屏幕而不是正常的gui.
我已经在某处读过这个答案,但我完全不明白:
我理解Windows使用curTimeAdjustment(156001 + - N)的值每curTimeIncrement(156001 100纳秒)递增时钟.但是当使用GetSystemTime读取时钟时,例程会在156001纳秒*100的时间间隔内插入以产生指示的精度吗?
有人可以试着向我解释一下吗?
是什么curTimeIncrement,curTimeAdjustment以及Windows如何做到这一点?
这对获得准确时间有什么影响?
这是真的只适用于Windows 7或其他操作系统Win8,Linux等等吗?
我编写了一个计时器,用于衡量任何多线程应用程序中特定代码的性能.在下面的计时器中,它还将使用x毫秒的数量填充地图.我将使用这张地图作为我的直方图的一部分来进行进一步的分析,比如这几分钟的调用百分比等等.
public static class StopWatch {
public static ConcurrentHashMap<Long, Long> histogram = new ConcurrentHashMap<Long, Long>();
/**
* Creates an instance of the timer and starts it running.
*/
public static StopWatch getInstance() {
return new StopWatch();
}
private long m_end = -1;
private long m_interval = -1;
private final long m_start;
private StopWatch() {
m_start = m_interval = currentTime();
}
/**
* Returns in milliseconds the amount of time that has elapsed since the timer was created. If the
* …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个间接与视频同步的计时器.当starttimer被点击,就应该开始我的定时器和发痒每一秒.
这是过程:
1. Start the video
2. At a certain time in video, click to start the timer
3. Timer starts from 00:00:00 and should tickle each second.
4. If the video is forwarded by `n` seconds timer should be 'timer+n` seconds. Same for the case, when video is rewinded - `timer-n'
Run Code Online (Sandbox Code Playgroud)
但我的计时器,功能不正常.它工作得很好,当我启动计时器但是当我向前推进n几秒钟时,它有时会过去n,有时候会被n+1或者n+2当我倒回n它时它会自行返回.
我只是无法得到正确的逻辑.
starttimer单击时调用:(从00:00:00开始计时)
var mtimer = 0;
$('#starttimer').click(function() { // Starts the clock
playing = …Run Code Online (Sandbox Code Playgroud)