forked from amal/AzaThread
-
Notifications
You must be signed in to change notification settings - Fork 4
/
SimpleThread.php
111 lines (96 loc) · 2.11 KB
/
SimpleThread.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace Aza\Components\Thread;
use Closure;
/**
* Simple API for closure thread
*
* @project Anizoptera CMF
* @package system.thread
* @author Amal Samally <amal.samally at gmail.com>
* @license MIT
*/
class SimpleThread extends Thread
{
/**
* Whether the thread will wait for next tasks.
* Preforked threads are always multitask.
*
* @see prefork
*/
protected $multitask = false;
/**
* Perform pre-fork, to avoid wasting resources later.
* Preforked threads are always multitask.
*
* @see multitask
*/
protected $prefork = false;
/**
* Maximum timeout for master to wait for the job results
* (in seconds, can be fractional).
* Set it to less than one, to disable.
*/
protected $timeoutMasterResultWait = 10;
/**
* Callable
*
* @var callable
*/
protected $callable;
/**
* Creates new thread with closure
*
* @param callable|Closure $callable <p>
* Thread closure (callable)
* </p>
* @param array $options [optional] <p>
* Thread options (array [property => value])
* </p>
* @param bool $debug [optional] <p>
* Whether to output debugging information
* </p>
*
* @return static
*/
public static function create($callable, array $options = null, $debug = false)
{
$options || $options = array();
$options['callable'] = $callable;
return new static(null, null, $debug, $options);
}
/**
* Prepares closure (only in PHP >= 5.4.0)
*
* @codeCoverageIgnore
*/
protected function onLoad()
{
// Prepare closure
$callable = $this->callable;
if ($callable instanceof Closure && method_exists($callable, 'bindTo')) {
/** @noinspection PhpUndefinedMethodInspection */
$callable->bindTo($this, $this);
}
}
/**
* Callable cleanup
*/
protected function onCleanup()
{
$this->callable = null;
}
/**
* Main processing. You need to override this method.
* Use {@link getParam} method to get processing parameters.
* Returned result will be available via {@link getResult}
* in the master process.
*
* @internal
*/
function process()
{
return call_user_func_array(
$this->callable, $this->getParams()
);
}
}