File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33 * This example shows how a PeriodicTimer can be used to schedule ticks
44 * forever, combined with an IndividualTimer that will cause the loop to halt
55 * after a specified period.
6+ *
7+ * To articulate the use of asynchronous methods, a "tiny tick timer" is
8+ * introduced that runs very frequently, alongside the countdown. You can
9+ * imagine the concurrent timers being used to execute actual workloads.
610 */
711
812use Gt \Async \Loop ;
1317
1418$ stopTimer = new IndividualTimer (5 );
1519$ periodicTimer = new PeriodicTimer (1 , true );
20+ $ tinyTickTimer = new PeriodicTimer (0.1 , true );
1621$ countdownNumber = 5 ;
1722
1823$ loop = new Loop ();
2126 echo "Countdown: $ countdownNumber " , PHP_EOL ;
2227 $ countdownNumber --;
2328});
29+ $ tinyTickTimer ->addCallback (function () {
30+ echo ". " ;
31+ });
2432$ stopTimer ->addCallback (function () use ($ loop ) {
2533 echo "LIFT OFF! " , PHP_EOL ;
2634 $ loop ->halt ();
2735});
2836
2937$ loop ->addTimer ($ periodicTimer );
38+ $ loop ->addTimer ($ tinyTickTimer );
3039$ loop ->addTimer ($ stopTimer );
3140echo "Starting... " , PHP_EOL ;
3241$ loop ->run ();
You can’t perform that action at this time.
0 commit comments