-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimesSubscriber.php
49 lines (39 loc) · 1.08 KB
/
PrimesSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
require_once 'Base.php';
class PrimesSubscriber extends Base
{
private $timeout;
private $redis;
public function __construct($timeout)
{
parent::__construct();
$this->timeout = $timeout;
$this->redis = $this->getRedis();
$this->subscriber();
}
public function subscriber()
{
echo "Prime numbers Subscriber started...\n";
if ($this->redis) {
$i = 0;
echo "Reading from channel \"Fibonacci\"\n";
while (true) {
$curNumber = $this->redis->lpop('prime');
if ($curNumber) {
var_dump($curNumber);
++$i;
$this->updateCount('count_prime', $i);
$this->updateSum($curNumber);
}
usleep($this->timeout * 1000);
}
echo "\n";
}
}
}
// "t" stands for Timeout
$options = getopt('t:');
if (!isset($options['t'])) {
exit("Usage: php <filename> -t [int]\n");
}
$generator = new PrimesSubscriber((int)$options['t']);