-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess-queue.php
More file actions
80 lines (63 loc) · 2.05 KB
/
Copy pathprocess-queue.php
File metadata and controls
80 lines (63 loc) · 2.05 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
<?php
// +----------------------------------------------------------------------
// | Program:multi-process
// +----------------------------------------------------------------------
// | Date: 2023/3/1 0001
// +----------------------------------------------------------------------
// | Author: js
// +----------------------------------------------------------------------
// | CreatedBy: phpStorm
// +----------------------------------------------------------------------
include dirname(__DIR__) . '/src/autoload.php';
use phpth\process\Process;
use phpth\process\supply\Call;
use phpth\process\supply\Status;
$p = new Process();
$data = [
'child data 1',
'child data 2',
'child data 3'
];
$que = Process::getIpcQueue('./queue.sv');
$que->setBlock(true);
// Supports all types except objects
$que->push([1]);
$que->push(null);
$que->push(false);
$que->push('');
$que->push('123');
$que->push('456');
$que->push('789');
$que->push('qwe');
$que->push('asd');
$que->push('zxc');
/*
//it will throw exception
$que->push(new class{})
$que->push([new class{}])
*/
//$que->push(new class{});
//$que->push(['sgsd']);
//$que->push([new class{}]);
$e = $p->runCall(function (int $processNo, mixed $param){
// child process execute: do something
// if you wang get parent process id, can do this
global $maiPid;
echo 'parent process id: '.$maiPid, PHP_EOL;
// callback function first param is process no, second param
echo "current process no: {$processNo}, process param: ".print_r($param).PHP_EOL;
sleep(1);
$que = Process::getIpcQueue('./queue.sv');
$que->setBlock(true);
$queueData = $que->pop();
echo 'get queue data: '.var_export($queueData, true).PHP_EOL;
},
$data,
Call::PROCESS_DATA_DISPATCH // Automatically start the process according to the number of array elements and split an element passed in $data into the second parameter of the callback function
);
foreach($e->wait(true, 1.5) as $processNo=> $status){
/**@var Status $status */
if(!$status->run){
echo $processNo.'stopped'.PHP_EOL;
}
}