/
train.php
81 lines (71 loc) · 2.46 KB
/
train.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
<?php
header("content-type:text/html;charset=utf-8");
class Server
{
private $serv;
public function __construct() {
$this->serv = new swoole_server("192.168.1.120", 8888);
$this->serv->set(array(
'worker_num' => 1,
'daemonize' => false,
'max_request' => 10000,
'dispatch_mode' => 2,
'debug_mode'=> 1,
'db_uri' => 'mysql:host=127.0.0.1;dbname=ystcrowds',
'db_user' => 'test',
'db_passwd' => 'test',
'task_worker_num' => 1
));
$this->serv->on('Start', array($this, 'onStart'));
$this->serv->on('Connect', array($this, 'onConnect'));
$this->serv->on('Receive', array($this, 'onReceive'));
$this->serv->on('Close', array($this, 'onClose'));
// bind callback
$this->serv->on('Task', array($this, 'onTask'));
$this->serv->on('Finish', array($this, 'onFinish'));
$this->serv->start();
}
public function onStart( $serv ) {
echo "Start\n";
}
public function onConnect( $serv, $fd, $from_id ) {
echo "Client {$fd} connect\n";
}
public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
echo "Get Message From Client {$fd}:{$data}\n";
// send a task to task worker.
$param = array(
'fd' => $fd,
'source' => $data
);
$serv->task( json_encode( $param ) );
echo "Continue Handle Worker\n";
}
public function onClose( $serv, $fd, $from_id ) {
echo "Client {$fd} close connection\n";
}
public function onTask($serv,$task_id,$from_id, $data) {
$data_arr=json_decode($data,true);
$source_data=$data_arr['source'];
$source = explode(' ', $source_data);
$trainid = $source[1];
$command = './tools/preprocess.py --id '.$trainid;
exec($command);
return "Task {$task_id}'s result";
}
public function onFinish($serv,$task_id, $data) {
}
/**
* send message by swoole
* @param string $content the command
* return boolean true if shut down the swoole_client successfully
*/
private function sendtagbyswoole($content){
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
$client->connect('127.0.0.1', 8888, 0.5, 0);
$client->send($content);
return $client -> close();
}
}
$server = new Server();
$server->start();