1
0
mirror of https://github.com/danog/amp.git synced 2024-12-02 17:37:50 +01:00
amp/examples/003_scheduling_func.php

32 lines
1.3 KiB
PHP
Raw Normal View History

2014-08-02 07:35:42 +02:00
<?php
require __DIR__ . '/../vendor/autoload.php';
2014-09-23 04:38:32 +02:00
Amp\run(function() {
2014-08-02 07:35:42 +02:00
$ticker = function() {
$now = time();
$vowel = ($now % 2) ? 'i' : 'o';
echo "t{$vowel}ck ", $now, "\n";
};
// Execute the specified callback ASAP in the next event loop iteration. There is no
// need to clear an "immediately" watcher after execution. The Reactor will automatically
// garbage collect resources associated with one-time events after they finish executing.
2014-09-23 04:38:32 +02:00
Amp\immediately($ticker);
2014-08-02 07:35:42 +02:00
// Execute every $msInterval milliseconds until the resulting $watcherId is cancelled.
// At some point in the future we need to cancel this watcher or our program will never end.
2014-09-23 04:38:32 +02:00
$repeatingWatcherId = Amp\repeat($ticker, $msInterval = 1000);
2014-08-02 07:35:42 +02:00
// Five seconds from now let's cancel the repeating ticker we just registered
2014-09-23 04:38:32 +02:00
Amp\once(function() use ($repeatingWatcherId) {
Amp\cancel($repeatingWatcherId);
2014-08-02 07:35:42 +02:00
echo "Cancelled repeating ticker\n";
}, $msDelay = 5000);
// After about five seconds the program will exit on its own. Why? This happens because in
// that time frame we will have cancelled the repeating watcher we registered using repeat()
// and the two one-off events (immediately() + once()) are automatically garbage collected
// by the Reactor after they execute.
});