-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonBridge.php
More file actions
executable file
·615 lines (521 loc) · 16.9 KB
/
Copy pathPythonBridge.php
File metadata and controls
executable file
·615 lines (521 loc) · 16.9 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<?php
namespace Python_In_PHP;
use Exception;
use WebSocket\Client;
/**
* A bridge for working with Python
*/
class PythonBridge
{
private string $host;
private int $port;
private string $wsUri;
private string $working_directory;
private string $python_script;
private string $python_binary;
private bool $debug;
private ?Client $client = null;
private bool $isConnected = false;
private mixed $process;
private array $object_references = [];
private array $async_operations = [];
private int $timeout;
private array $pipes;
/**
* @param array{
* debug?: bool,
* timeout?: int,
* host?: string,
* port?: int,
* working_directory?: string,
* python_binary?: string
* } $options
*/
public function __construct(array $options = [])
{
$this->host = $options['host'] ?? '127.0.0.1';
$this->port = $options['port'] ?? $this->getFreePort();
$this->wsUri = "ws://{$this->host}:{$this->port}/";
$this->working_directory = $options['working_directory'] ?? getcwd();
$this->python_script = __DIR__ . '/python_server/python_worker.py';
$this->python_binary = $options['python_binary'] ?? realpath(__DIR__ . '/../python_bin') . '/python' . (PHP_OS_FAMILY == 'Windows' ? '.exe' : '');
$this->debug = $options['debug'] ?? false;
$this->timeout = $options['timeout'] ?? 36000;
}
public function __destruct()
{
$this->disconnect();
$this->stop();
}
/**
* @param array{
* debug?: bool,
* timeout?: int,
* host?: string,
* port?: int,
* working_directory?: string,
* python_binary?: string
* } $options
*/
static function startOrGetRunning(array $options = [])
{
global $__python_bridge;
if (isset($__python_bridge)) {
return $__python_bridge;
}
$__python_bridge = new self($options);
return $__python_bridge;
}
static function getInstance(): ?self
{
global $__python_bridge;
if (isset($__python_bridge)) {
return $__python_bridge;
}
return null;
}
function isStarted(): bool
{
$fp = @fsockopen($this->host, $this->port, $errno, $errstr, 0.005);
if ($fp) {
fclose($fp);
return true;
}
return false;
}
function isRunning(): bool
{
return $this->ping() == 'pong';
}
private function log($message): void
{
if ($this->debug) {
echo "[DEBUG] " . date('Y-m-d H:i:s') . " - $message\n";
}
}
private function startServer(): void
{
if ($this->isStarted()) {
$this->log("The server is already running on the port {$this->port}");
return;
}
$scriptPath = realpath($this->python_script);
if (!$scriptPath) {
throw new Exception("⚠️ Python script was not found: {$this->python_script}");
}
$verbose = $this->debug ? '--verbose 1' : '';
$cmd = "{$this->python_binary} \"$scriptPath\" --host {$this->host} --port {$this->port} $verbose";
$this->log("Starting the Python server: $cmd");
if (PHP_OS_FAMILY === 'Windows') {
$redirect = $this->debug ? '' : '>nul 2>&1';
$command = 'cmd /C "cd /D "' . $this->working_directory . '" && ' . $cmd . ' ' . $redirect . '"';
$this->process = $process = proc_open($command, [], $pipes);
}
else {
$redirect = $this->debug ? '' : '> /dev/null 2>&1';
$command = "cd \"{$this->working_directory}\" && {$cmd} $redirect";
$this->process = $process = proc_open($command, [], $pipes);
}
$this->pipes = $pipes;
register_shutdown_function(function() {
$this->disconnect();
$this->stop();
});
// Wait for the server to start, with a timeout
$startupTimeout = 30; // seconds
$deadline = microtime(true) + $startupTimeout;
while (!$this->isStarted()) {
$status = proc_get_status($this->process);
if (!$status['running']) {
throw new Exception("❌ The Python process exited unexpectedly before the server started");
}
if (microtime(true) >= $deadline) {
$this->stop();
throw new Exception("❌ The Python server did not start within {$startupTimeout} seconds");
}
usleep(50000); // 50ms
}
$this->log("✅ The Python server was started successfully");
}
private function getFreePort(): int {
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, '127.0.0.1', 0); // 0 = OS will choose a free port
socket_getsockname($socket, $addr, $port);
socket_close($socket);
return $port;
}
private function connectToServer(): void
{
if ($this->isConnected) {
return;
}
if (!$this->isStarted()) {
$this->startServer();
}
$this->log("Establishing a WebSocket connection to {$this->wsUri}");
try {
$this->client = new Client($this->wsUri, [
'timeout' => $this->timeout,
]);
$this->isConnected = true;
}
catch (Exception $e) {
throw new Exception("❌ Unable to connect to the WebSocket server: " . $e->getMessage());
}
$this->log("✅ The connection established");
}
private function stop()
{
if (!is_resource($this->process)) {
return;
}
$status = proc_get_status($this->process);
$pid = $status['pid'];
// Close all pipes first, otherwise proc_close may hang
foreach ($this->pipes ?? [] as $pipe) {
if (is_resource($pipe)) {
fclose($pipe);
}
}
if ($status['running']) {
if (PHP_OS_FAMILY === 'Windows') {
exec("taskkill /F /T /PID {$pid}");
}
else {
// Send SIGTERM to the shell process by its exact PID.
// Using -$pid (process group) would fail because proc_open does
// not create a new process group, so the PGID never equals $pid.
posix_kill($pid, SIGTERM);
$deadline = microtime(true) + 3;
while (microtime(true) < $deadline) {
usleep(100000); // 100ms
$check = proc_get_status($this->process);
if (!$check['running']) {
break;
}
}
// If still alive — SIGKILL
$check = proc_get_status($this->process);
if ($check['running']) {
posix_kill($pid, SIGKILL);
}
}
}
proc_close($this->process);
$this->process = null;
}
private function processResult($result)
{
if (is_array($result) && isset($result['__python_ref__'])) {
$obj = new PythonObject($this, $result);
$this->object_references[$result['obj_id']] = $obj;
return $obj;
}
elseif (is_array($result)) {
$processedArray = [];
foreach ($result as $key => $value) {
$processedArray[$key] = $this->processResult($value);
}
return $processedArray;
}
return $result;
}
/**
* Execution of an asynchronous operation
*/
public function async(callable $operation)
{
$operationId = uniqid('async_');
$this->async_operations[$operationId] = [
'operation' => $operation,
'status' => 'pending',
'result' => null,
'error' => null
];
return $operationId;
}
/**
* Waiting for an asynchronous operation to complete
*/
public function await($operationId, $timeout = null)
{
if (!isset($this->async_operations[$operationId])) {
throw new Exception("Operation $operationId not found");
}
$timeout = $timeout ?? $this->timeout;
$startTime = microtime(true);
while ($this->async_operations[$operationId]['status'] === 'pending') {
if (microtime(true) - $startTime > $timeout) {
throw new Exception("Operation $operationId timed out after {$timeout} seconds");
}
usleep(100000); // 100ms
}
if ($this->async_operations[$operationId]['error']) {
throw new Exception($this->async_operations[$operationId]['error']);
}
return $this->async_operations[$operationId]['result'];
}
/**
* Executing an asynchronous Python function call
*/
public function asyncCall($function, $args = [], $kwargs = [])
{
return $this->async(function() use ($function, $args, $kwargs) {
return $this->call($function, $args, $kwargs);
});
}
/**
* Performing an asynchronous call to a Python object method
*/
public function asyncCallMethod($objId, $method, $args = [], $kwargs = [])
{
return $this->async(function() use ($objId, $method, $args, $kwargs) {
return $this->callMethod($objId, $method, $args, $kwargs);
});
}
/**
* Working with Python's context manager
*/
public function with($objId, callable $callback)
{
try {
// Enter context
$this->execute('context_enter', ['obj_id' => $objId]);
// Callback execution
$result = $callback();
// Exit the context
$this->execute('context_exit', ['obj_id' => $objId]);
return $result;
}
catch (Exception $e) {
// In case of an error, we also exit the context
try {
$this->execute('context_exit', ['obj_id' => $objId]);
}
catch (Exception $exitError) {
$this->log("Error during context exit: " . $exitError->getMessage());
}
throw $e;
}
}
/**
* Asynchronous work with Python's context manager
*/
public function asyncWith($objId, callable $callback)
{
return $this->async(function() use ($objId, $callback) {
return $this->with($objId, $callback);
});
}
/**
* Get information about an object method
*/
public function getMethodInfo($objId, $method)
{
$objRef = $this->object_references[$objId] ?? null;
if (!$objRef) {
throw new Exception("Object $objId not found");
}
foreach ($objRef->methods as $methodInfo) {
if ($methodInfo['name'] === $method) {
return $methodInfo;
}
}
throw new Exception("Method $method not found in object $objId");
}
/**
* Check whether a method is asynchronous
*/
public function isAsyncMethod($objId, $method)
{
$methodInfo = $this->getMethodInfo($objId, $method);
return $methodInfo['is_async'] ?? false;
}
/**
* Execute a command with support for asynchronous operations
*/
public function execute($command, $args = [], $module = null)
{
if (!$this->isConnected) {
$this->connectToServer();
}
$payload = json_encode([
'command' => $command,
'args' => $args,
'module' => $module,
'id' => uniqid()
]);
$this->log("Sending command: $payload");
$this->client->send($payload);
$response = $this->client->receive();
$this->log("Response received: " . substr($response, 0, 2000));
$result = json_decode($response, true, 10000);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception("JSON parsing error: " . json_last_error_msg());
}
if (isset($result['error']) && $result['error']) {
if (!empty($result['traceback'])) print_r($result['traceback']);
if (!empty($result['data'])) print_r($result['data']);
throw new Exception("Python error: " . $result['error']);
}
// Process the result to create object references
$processedResult = $this->processResult($result['result'] ?? null);
return $processedResult;
}
/**
* Process arguments for passing to Python
*/
private function processArguments($args)
{
if (is_array($args)) {
return array_map(function($arg) {
if ($arg instanceof PythonObject) {
return $arg->toArray();
}
return $arg;
}, $args);
}
return $args;
}
/**
* Process kwargs for passing to Python
*/
private function processKwargs($kwargs)
{
if (is_array($kwargs)) {
$result = [];
foreach ($kwargs as $key => $value) {
if ($value instanceof PythonObject) {
$result[$key] = $value->toArray();
} else {
$result[$key] = $value;
}
}
return $result;
}
return $kwargs;
}
public function call($function, $args = [], $kwargs = [])
{
return $this->execute('call', [
'function' => $function,
'args' => $this->processArguments($args),
'kwargs' => $this->processKwargs($kwargs)
]);
}
public function eval($code)
{
return $this->execute('eval', ['code' => $code]);
}
public function exec($code)
{
return $this->execute('exec', ['code' => $code]);
}
public function importModule($moduleName, $alias = null)
{
return $this->execute('import', ['module' => $moduleName, 'alias' => $alias]);
}
/**
* Call a method on a referenced object
*/
public function callMethod($objId, $method, $args = [], $kwargs = [])
{
return $this->execute('call_method', [
'obj_id' => $objId,
'method' => $method,
'args' => $this->processArguments($args),
'kwargs' => $this->processKwargs($kwargs)
]);
}
/**
* Call an object as a function
*/
public function callObject($objId, $args = [], $kwargs = [])
{
return $this->execute('call_object', [
'obj_id' => $objId,
'args' => $this->processArguments($args),
'kwargs' => $this->processKwargs($kwargs)
]);
}
/**
* Get an attribute of an object
*/
public function getAttribute($objId, $attribute)
{
return $this->execute('get_attribute', [
'obj_id' => $objId,
'attribute' => $attribute
]);
}
/**
* Release an object from Python memory
*/
public function releaseObject($objId)
{
try {
$result = $this->execute('release_object', ['obj_id' => $objId]);
unset($this->object_references[$objId]);
return $result;
} catch (Exception $e) {
$this->log("Error releasing object $objId: " . $e->getMessage());
throw $e;
}
}
/**
* Convert an object to a string
*/
public function objectToString($objId): string
{
return $this->execute('to_string', ['obj_id' => $objId]);
}
/**
* Get the list of all objects in Python memory
*/
public function listObjects(): array
{
return $this->execute('list_objects');
}
/**
* Ping the server
*/
public function ping(): string
{
return $this->execute('ping');
}
/**
* Get the list of loaded modules
*/
public function listModules(): array
{
return $this->execute('list_modules');
}
/**
* Check whether an object is a generator
*/
public function isGenerator($objId): bool
{
return $this->execute('is_generator', ['obj_id' => $objId]);
}
public function getModuleNamesInPackages(array $packages)
{
return $this->execute('get_module_names_in_packages', ['packages' => $packages]);
}
public function inspectModules(array $modules)
{
return $this->execute('inspect_modules', ['modules' => $modules]);
}
public function getMethodsAndProperties($objId)
{
return $this->execute('get_methods_and_properties', ['obj_id' => $objId]);
}
private function disconnect()
{
if ($this->client) {
$this->client->close();
$this->client = null;
$this->isConnected = false;
$this->log("WebSocket connection closed");
}
}
}