-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathThread.php
181 lines (170 loc) · 4.2 KB
/
Thread.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
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
<?php
/**
* @name Thread.php
* @author Alex Snet ( [email protected] )
* @author Moisés Márquez (https://github.com/mmarquez)
* @version 1.1.2
* @copyright free for use
* @category forking
* @category system
*/
class Thread{
public $child = false;
private $pid = false;
private $file;
private $tmpdir;
private $children = array();
private $vars = array();
public function __construct($argv,$argc){
$GET = array();
$this->pid = getmypid();
if ($argc != null){
$this->file = str_replace(' ','\ ',getcwd()).'/'.$argv[0];
}else{
$this->file = $argv;
}
for($i=1;$i<$argc;$i++){
if(substr($argv[$i],0,1)=='-' && ( isset($argv[($i+1)]) && substr($argv[($i+1)],0,1)!=='-')){
$GET[substr($argv[$i],1)] = $argv[($i+1)];
$i++;
}else{
$GET[substr($argv[$i],1)] = true;
}
}
$this->vars = $GET;
if(isset($this->vars['tmpdir'])){
$this->child = $this->vars['pid'];
$this->tmpdir = $this->vars['tmpdir'];
}else{
$this->tmpdir = '/tmp/'.md5($this->file . time());
mkdir($this->tmpdir);
$limitTime = "60";
$now = 0;
while (!is_dir($this->tmpdir)){
if ($now < $limitTime){
sleep(1);
$now++;
}
}
}
if (!is_dir("{$this->tmpdir}/vars")){
mkdir($this->tmpdir.'/vars');
}
}
public function __destruct(){
if($this->child == false){
if(file_exists($this->tmpdir.'/vars/')){
$vars = scandir($this->tmpdir.'/vars/');
foreach($vars as $var){
if(strlen($var)>3){
unlink($this->tmpdir.'/vars/'.$var);
}
}
rmdir($this->tmpdir.'/vars/');
}
if(file_exists($this->tmpdir.'/threads/')){
rmdir($this->tmpdir.'/threads/');
}
$outputs = scandir($this->tmpdir);
foreach ($outputs as $output){
if(strlen($output)>3){
unlink("{$this->tmpdir}/{$output}");
}
}
rmdir($this->tmpdir);
}else{
unlink("{$this->tmpdir}/threads/{$this->child}.pid");
}
}
/**
* Allows the communication between process.
* @param String $var The name of the variable to read
* @return String The value of the variable
*/
public function __get($var){
$val = false;
if(file_exists($this->tmpdir.'/vars/'.md5($var))){
$val = file_get_contents($this->tmpdir.'/vars/'.md5($var));
$val = unserialize($val);
}
return $val;
}
/**
* Allows the communication between processes.
* Send the value of the specified variable.
* @param String $var The name of the variable to write
* @param void* $val A serializable value to write
*/
public function __set($var,$val){
$vall = $this->{$var};
if($vall!=$val){
if ($fp = fopen($this->tmpdir.'/vars/'.md5($var), "w")){
if (flock($fp, LOCK_EX)){
fwrite($fp, serialize($val));
flock($fp, LOCK_UN);
}else{
$this->{$var} = $val;
}
fclose($fp);
}
}
}
/**
* Init the child processes
*/
public function startThreads($count=0){
mkdir($this->tmpdir.'/threads');
$limitTime = "60";
$now = 0;
while (!is_dir($this->tmpdir.'/threads')){
if ($now < $limitTime){
sleep(1);
$now++;
}
}
for($i=1;$i<=$count;$i++){
$pid = $this->tmpdir.'/threads/'.$i.'.pid';
$this->children[$i]['pid'] = popen("php {$this->file} -tmpdir {$this->tmpdir} -pid {$i} > {$pid} 2> {$this->tmpdir}_{$i}.log &",'r');
}
sleep(1);
}
/**
* Get the number of actual childs
*/
public function childs(){
$dd = 0;
$darr = scandir($this->tmpdir.'/threads/');
foreach($darr as $d){
if($d!='.' && $d!='..'){
$dd++;
}
}
return $dd;
}
/**
* Write the messages to a file in the temporal dir with the id of the child.
* Only works if the process is a child.
*/
public function write($messages){
if (is_object($messages)){
if ($this->child){
$str = json_encode($messages);
$f = fopen($this->tmpdir.'/'.$this->child.'.pid','a');
fwrite($f,$str);
fclose($f);
}
}
}
/**
* Try to read the information written by the process.
* @param int $pid The pid of the process to read
* @return Object|String Return an object with the written information or false in error.
*/
public function read($pid){
if ((!$this->child) && (is_readable("{$this->tmpdir}/{$pid}.pid"))){
return json_decode(file_get_contents("{$this->tmpdir}/{$pid}.pid"));
}else{
return false;
}
}
}