1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Work |
5
|
|
|
* |
6
|
|
|
* Cooperative multitasking via coroutines. |
7
|
|
|
* |
8
|
|
|
* @package core |
9
|
|
|
* @author [email protected] |
10
|
|
|
* @reference http://nikic.github.io/2012/12/22/Cooperative-multitasking-using-coroutines-in-PHP.html |
11
|
|
|
* @copyright Caffeina srl - 2015 - http://caffeina.it |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
// version_compare(PHP_VERSION, '5.5.0', '>=') |
15
|
|
|
// or trigger_error('Work module need PHP 5.5 or later.',E_USER_ERROR); |
16
|
|
|
|
17
|
|
|
class Work { |
|
|
|
|
18
|
|
|
use Module; |
19
|
|
|
|
20
|
|
|
protected static $pool = null; |
21
|
|
|
protected static $workers; |
22
|
|
|
protected static $lastID = 0; |
23
|
|
|
|
24
|
|
|
public static function add($id, $job=null){ |
25
|
|
|
self::$pool or ( self::$pool = new \SplQueue() ); |
|
|
|
|
26
|
|
|
if(is_callable($id) && $job===null){ |
27
|
|
|
$job = $id; |
28
|
|
|
$id = ++self::$lastID; |
29
|
|
|
} |
30
|
|
|
$task = new TaskCoroutine($id, $job instanceof \Generator ? $job : $job()); |
31
|
|
|
self::$workers[$id] = $task; |
32
|
|
|
self::$pool->enqueue($task); |
33
|
|
|
return $task; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function send($id,$passValue) { |
37
|
|
|
isset(self::$workers[$id]) && self::$workers[$id]->pass($passValue); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function run(){ |
41
|
|
|
self::$pool or ( self::$pool = new \SplQueue() ); |
|
|
|
|
42
|
|
|
while (!self::$pool->isEmpty()) { |
43
|
|
|
$task = self::$pool->dequeue(); |
44
|
|
|
$task->run(); |
45
|
|
|
if ($task->complete()) { |
46
|
|
|
unset(self::$workers[$task->id()]); |
47
|
|
|
} else { |
48
|
|
|
self::$pool->enqueue($task); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Defer callback execution after script execution |
55
|
|
|
* @param callable $callback The deferred callback |
56
|
|
|
*/ |
57
|
|
|
public static function after(callable $callback){ |
58
|
|
|
static::$inited_shutdown || static::install_shutdown(); |
59
|
|
|
Event::on('core.shutdown', $callback); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Single shot defer handeler install |
64
|
|
|
*/ |
65
|
|
|
protected static function install_shutdown(){ |
66
|
|
|
if (static::$inited_shutdown) return; |
67
|
|
|
|
68
|
|
|
// Disable time limit |
69
|
|
|
set_time_limit(0); |
70
|
|
|
|
71
|
|
|
// HHVM support |
72
|
|
|
if(function_exists('register_postsend_function')){ |
73
|
|
|
register_postsend_function(function(){ |
74
|
|
|
Event::trigger('core.shutdown'); |
75
|
|
|
}); |
76
|
|
|
} else if(function_exists('fastcgi_finish_request')) { |
77
|
|
|
register_shutdown_function(function(){ |
78
|
|
|
fastcgi_finish_request(); |
79
|
|
|
Event::trigger('core.shutdown'); |
80
|
|
|
}); |
81
|
|
|
} else { |
82
|
|
|
register_shutdown_function(function(){ |
83
|
|
|
Event::trigger('core.shutdown'); |
84
|
|
|
}); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
static::$inited_shutdown = true; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
class TaskCoroutine { |
|
|
|
|
92
|
|
|
|
93
|
|
|
protected $id; |
94
|
|
|
protected $coroutine; |
95
|
|
|
protected $passValue = null; |
96
|
|
|
protected $beforeFirstYield = true; |
97
|
|
|
protected static $inited_shutdown = false; |
98
|
|
|
|
99
|
|
|
public function __construct($id, \Generator $coroutine) { |
100
|
|
|
$this->id = $id; |
101
|
|
|
$this->coroutine = $coroutine; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function id() { |
105
|
|
|
return $this->id; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function pass($passValue) { |
109
|
|
|
$this->passValue = $passValue; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function run() { |
113
|
|
|
if ($this->beforeFirstYield) { |
114
|
|
|
$this->beforeFirstYield = false; |
115
|
|
|
return $this->coroutine->current(); |
116
|
|
|
} else { |
117
|
|
|
$retval = $this->coroutine->send($this->passValue); |
118
|
|
|
$this->passValue = null; |
119
|
|
|
return $retval; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function complete() { |
124
|
|
|
return ! $this->coroutine->valid(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.