-
-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathSwoole.php
More file actions
173 lines (138 loc) · 4.2 KB
/
Swoole.php
File metadata and controls
173 lines (138 loc) · 4.2 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace Rubix\ML\Backends;
use Rubix\ML\Backends\Tasks\Task;
use Rubix\ML\Specifications\ExtensionIsLoaded;
use Rubix\ML\Specifications\SwooleExtensionIsLoaded;
use RuntimeException;
use Swoole\Atomic;
use Swoole\Process;
use function Swoole\Coroutine\run;
/**
* Swoole
*
* Works both with Swoole and OpenSwoole.
*
* @category Machine Learning
* @package Rubix/ML
*/
class Swoole implements Backend
{
/**
* The queue of tasks to be processed in parallel.
*/
protected array $queue = [];
private int $cpus;
private int $hasIgbinary;
public function __construct()
{
SwooleExtensionIsLoaded::create()->check();
$this->cpus = swoole_cpu_num();
$this->hasIgbinary = ExtensionIsLoaded::with('igbinary')->passes();
}
/**
* Queue up a deferred task for backend processing.
*
* @internal
*
* @param Task $task
* @param callable(mixed,mixed):void $after
* @param mixed $context
*/
public function enqueue(Task $task, ?callable $after = null, $context = null) : void
{
$this->queue[] = function () use ($task, $after, $context) {
$result = $task();
if ($after) {
$after($result, $context);
}
return $result;
};
}
/**
* Process the queue and return the results.
*
* @internal
*
* @return mixed[]
*/
public function process() : array
{
$results = [];
$maxMessageLength = new Atomic(0);
$workerProcesses = [];
$currentCpu = 0;
foreach ($this->queue as $index => $queueItem) {
$workerProcess = new Process(
function (Process $worker) use ($maxMessageLength, $queueItem) {
$serialized = $this->serialize($queueItem());
$serializedLength = strlen($serialized);
$currentMaxSerializedLength = $maxMessageLength->get();
if ($serializedLength > $currentMaxSerializedLength) {
$maxMessageLength->set($serializedLength);
}
$worker->exportSocket()->send($serialized);
},
// redirect_stdin_and_stdout
false,
// pipe_type
SOCK_DGRAM,
// enable_coroutine
true,
);
$workerProcess->setAffinity([$currentCpu]);
$workerProcess->setBlocking(false);
$workerProcess->start();
$workerProcesses[$index] = $workerProcess;
$currentCpu = ($currentCpu + 1) % $this->cpus;
}
run(function () use ($maxMessageLength, &$results, $workerProcesses) {
foreach ($workerProcesses as $index => $workerProcess) {
$status = $workerProcess->wait();
if (0 !== $status['code']) {
throw new RuntimeException('Worker process exited with an error');
}
$socket = $workerProcess->exportSocket();
if ($socket->isClosed()) {
throw new RuntimeException('Coroutine socket is closed');
}
$maxMessageLengthValue = $maxMessageLength->get();
$receivedData = $socket->recv($maxMessageLengthValue);
$unserialized = $this->unserialize($receivedData);
$results[] = $unserialized;
}
});
return $results;
}
/**
* Flush the queue
*/
public function flush() : void
{
$this->queue = [];
}
private function serialize(mixed $data) : string
{
if ($this->hasIgbinary) {
return igbinary_serialize($data);
}
return serialize($data);
}
private function unserialize(string $serialized) : mixed
{
if ($this->hasIgbinary) {
return igbinary_unserialize($serialized);
}
return unserialize($serialized);
}
/**
* Return the string representation of the object.
*
* @internal
*
* @return string
*/
public function __toString() : string
{
return 'Swoole';
}
}