You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can addPeriodicTimer() run on absolute time intervals? #9
Can I tell addPeriodicTimer() to run on intervals, that are exactly 1 second apart?
Currently, if I run the following code, I get the following output:
Im guessing addPeriodicTimer() is acting as a recursive addTimer() and the bit of time it takes to run is responsible for the 'skew' in time of each iteration. Can you give me advice on how to make it run uniformly? Thank you.
I don't think this is possible given the nature. The event loop executes chunks of code when the event loop is free and receives an event (I/O, timeout, tick).
To get closer to this you would need a dedicated core via a separate process or thread that its only job is to watch this timer and rapidly delegate off to another context.
Thanks for the answer, Chris. Im unfortunately not cool enough to understand anything you wrote in the last paragraph of your reply. But I get how the nature of the event loop complicates things.
I want to share my workaround here:
public function numberBreakdown ($number) {
$whole = floor($number);
$fraction = $number - $whole;
}
public function Timer ($interval = 1) {
$this->loop->addTimer($interval, function() {
echo microtime();
$interval = 1 - $this->numberBreakdown(microtime(true));
$this->Timer($interval);
});
}
The first function just returns the decimal part of a number.
The timer is now a simple one-off timer that is wrapped inside a function that calls itself repeatedly.
Every time it calls itself, a new $interval is passed which compensates for the 'skew' every cycle by subtracting the decimal part of the timestamp from the intended 1 second.
Activity
cboden commentedon Aug 13, 2014
I don't think this is possible given the nature. The event loop executes chunks of code when the event loop is free and receives an event (I/O, timeout, tick).
To get closer to this you would need a dedicated core via a separate process or thread that its only job is to watch this timer and rapidly delegate off to another context.
minwangdk commentedon Aug 14, 2014
Thanks for the answer, Chris. Im unfortunately not cool enough to understand anything you wrote in the last paragraph of your reply. But I get how the nature of the event loop complicates things.
I want to share my workaround here:
The first function just returns the decimal part of a number.
The timer is now a simple one-off timer that is wrapped inside a function that calls itself repeatedly.
Every time it calls itself, a new $interval is passed which compensates for the 'skew' every cycle by subtracting the decimal part of the timestamp from the intended 1 second.
Output:
0.81476100 1408059957...
0.00064400 1408059958...
0.00002600 1408059959...
0.00203700 1408059960...
0.00025600 1408059961...
0.99997200 1408059961...
0.99993000 1408059962...
0.00031100 1408059964...
0.00081800 1408059965...
0.00090900 1408059966...
0.00083200 1408059967...
0.99974900 1408059967...
0.00017200 1408059969...
0.00100600 1408059970...
0.00047100 1408059971...
0.00042900 1408059972...
0.99995300 1408059972...
0.00027100 1408059974...
Not exactly accurate but good enough for my purposes as long as it runs 10 thousand times in 10 thousand seconds.