forked from LinkedDestiny/swoole-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8d1acb
commit b02416b
Showing
7 changed files
with
337 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: lancelot | ||
* Date: 16-6-29 | ||
* Time: 下午8:35 | ||
*/ | ||
|
||
class BaseProcess | ||
{ | ||
|
||
private $process; | ||
|
||
public function __construct() | ||
{ | ||
$this->process = new swoole_process(array($this, 'run') , false , true); | ||
$this->process->signal(SIGTERM, function($signo) { | ||
echo "{$signo} shutdown.\n"; | ||
}); | ||
$this->process->daemon(true,true); | ||
var_dump($this->process); | ||
$this->process->start(); | ||
|
||
swoole_event_add($this->process->pipe, function ($pipe){ | ||
$data = $this->process->read(); | ||
echo "RECV: " . $data.PHP_EOL; | ||
}); | ||
} | ||
|
||
public function run($worker) | ||
{ | ||
swoole_timer_tick(1000, function() { | ||
$this->process->write("Hello"); | ||
}); | ||
|
||
swoole_process::wait(); | ||
} | ||
} | ||
|
||
new BaseProcess(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: lancelot | ||
* Date: 16-6-29 | ||
* Time: 下午10:34 | ||
*/ | ||
|
||
class BaseProcess | ||
{ | ||
|
||
private $process; | ||
|
||
private $process_list = []; | ||
private $worker_num = 3; | ||
|
||
public function __construct() | ||
{ | ||
$this->process = new swoole_process(array($this, 'run') , false , false); | ||
$this->process->useQueue(); | ||
$this->process->start(); | ||
|
||
} | ||
|
||
public function run() | ||
{ | ||
for($i=0;$i<$this->worker_num ; $i++){ | ||
$this->process_list[$i] = new swoole_process(array($this, 'task_run') , false , false); | ||
$this->process_list[$i]->useQueue(); | ||
$this->process_list[$i]->start(); | ||
} | ||
|
||
swoole_timer_tick(1000, function() { | ||
$this->process->push("Hello"); | ||
}); | ||
} | ||
|
||
public function task_run($worker) | ||
{ | ||
while(true) | ||
{ | ||
$data = $worker->pop(); | ||
var_dump($data); | ||
sleep(5); | ||
} | ||
} | ||
} | ||
|
||
new BaseProcess(); |
Submodule swoole-crontab
added at
9c1804
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: lancelot | ||
* Date: 16-6-18 | ||
* Time: 下午4:11 | ||
*/ | ||
|
||
class Client | ||
{ | ||
private $client; | ||
public function __construct() { | ||
$this->client = new swoole_client(SWOOLE_SOCK_TCP); | ||
} | ||
public function connect() { | ||
if( !$this->client->connect("127.0.0.1", 9501 , 1) ) { | ||
echo "Error: {$fp->errMsg}[{$fp->errCode}]\n"; | ||
} | ||
fwrite(STDOUT, "请输入消息:"); | ||
$msg = trim(fgets(STDIN)); | ||
$this->client->send( $msg ); | ||
sleep(1); | ||
$message = $this->client->recv(); | ||
echo "Get Message From Server:{$message}\n"; | ||
} | ||
public function test() { | ||
$this->client = new swoole_client(SWOOLE_SOCK_TCP); | ||
|
||
} | ||
} | ||
$client = new Client(); | ||
$client->connect(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
class Test | ||
{ | ||
public $index = 0; | ||
} | ||
|
||
class Server | ||
{ | ||
private $serv; | ||
private $test; | ||
|
||
public function __construct() { | ||
$this->serv = new swoole_server("0.0.0.0", 9501); | ||
$this->serv->set(array( | ||
'worker_num' => 8, | ||
'daemonize' => false, | ||
'max_request' => 10000, | ||
'dispatch_mode' => 2, | ||
'task_worker_num' => 8 | ||
)); | ||
$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 onClose( $serv, $fd, $from_id ) { | ||
echo "Client {$fd} close connection\n"; | ||
} | ||
|
||
public function onReceive( swoole_server $serv, $fd, $from_id, $data ) { | ||
echo "Get Message From Client {$fd}:{$data}\n"; | ||
$this->test = new Test(); | ||
var_dump($this->test); | ||
|
||
$serv->task( serialize($this->test) ); | ||
} | ||
|
||
public function onTask($serv,$task_id,$from_id, $data) { | ||
echo "This Task {$task_id} from Worker {$from_id}\n"; | ||
echo "Data: {$data}\n"; | ||
|
||
$data = unserialize($data); | ||
$data->index = 2; | ||
$this->test = new Test(); | ||
$this->test->index = 2; | ||
var_dump($data); | ||
var_dump($this->test); | ||
|
||
return "Finished"; | ||
} | ||
public function onFinish($serv,$task_id, $data) { | ||
echo "Task {$task_id} finish\n"; | ||
echo "Result: {$data}\n"; | ||
|
||
var_dump($this->test); | ||
} | ||
} | ||
$server = new Server(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
class MySQLPool | ||
{ | ||
private $serv; | ||
private $pdo; | ||
public function __construct() { | ||
$this->serv = new swoole_server("0.0.0.0", 9501); | ||
$this->serv->set(array( | ||
'worker_num' => 8, | ||
'daemonize' => false, | ||
'max_request' => 10000, | ||
'dispatch_mode' => 3, | ||
'debug_mode'=> 1 , | ||
'task_worker_num' => 8 | ||
)); | ||
$this->serv->on('WorkerStart', array($this, 'onWorkerStart')); | ||
$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 onConnect( $serv, $fd, $from_id ) { | ||
echo "Client {$fd} connect\n"; | ||
} | ||
public function onClose( $serv, $fd, $from_id ) { | ||
echo "Client {$fd} close connection\n"; | ||
} | ||
|
||
public function onWorkerStart( $serv , $worker_id) { | ||
//echo "onWorkerStart\n"; | ||
if($serv->taskworker){ | ||
$this->pdo = new PDO( | ||
"mysql:host=localhost;port=3306;dbname=Test", | ||
"root", | ||
"123456", | ||
array( | ||
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8';", | ||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | ||
PDO::ATTR_PERSISTENT => true | ||
) | ||
); | ||
echo "Task Worker\n"; | ||
} else { | ||
echo "Worker Process\n"; | ||
} | ||
|
||
|
||
} | ||
|
||
public function onReceive( swoole_server $serv, $fd, $from_id, $data ) { | ||
|
||
$task = [ | ||
'sql' => 'insert into user values (? , ?)', | ||
'params' => [ 1, 'swoole'], | ||
'fd' => $fd | ||
]; | ||
$serv->task(json_encode($task)); | ||
} | ||
|
||
public function onTask($serv,$task_id,$from_id, $data) { | ||
try{ | ||
$data = json_decode($data,true); | ||
|
||
$statement = $this->pdo->prepare($data['sql']); | ||
$statement->execute($data['params']); | ||
|
||
$serv->send($data['fd'] , "Insert succeed"); | ||
return "true"; | ||
} catch( PDOException $e ) { | ||
var_dump( $e ); | ||
return "false"; | ||
} | ||
} | ||
public function onFinish($serv,$task_id, $data) { | ||
var_dump("result: " + $data); | ||
} | ||
} | ||
new MySQLPool(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
class Test | ||
{ | ||
public $index = 0; | ||
} | ||
|
||
class Server | ||
{ | ||
private $serv; | ||
private $test; | ||
|
||
public function __construct() { | ||
$this->serv = new swoole_server("0.0.0.0", 9501); | ||
$this->serv->set(array( | ||
'worker_num' => 8, | ||
'daemonize' => false, | ||
'max_request' => 10000, | ||
'dispatch_mode' => 2, | ||
)); | ||
$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')); | ||
$this->serv->on('WorkerStart', array($this, 'onWorkerStart')); | ||
|
||
$this->serv->start(); | ||
} | ||
public function onStart( $serv ) { | ||
echo "Start\n"; | ||
} | ||
public function onConnect( $serv, $fd, $from_id ) { | ||
echo "Client {$fd} connect\n"; | ||
} | ||
public function onClose( $serv, $fd, $from_id ) { | ||
echo "Client {$fd} close connection\n"; | ||
} | ||
|
||
public function onWorkerStart( $serv , $worker_id) { | ||
if( $worker_id == 0 ) | ||
{ | ||
$this->test=new Test(); | ||
$this->test->index = 1; | ||
swoole_timer_tick(1000, array($this, 'onTick'), "Hello"); | ||
} | ||
} | ||
|
||
public function onReceive( swoole_server $serv, $fd, $from_id, $data ) { | ||
echo "Get Message From Client {$fd}:{$data}\n"; | ||
|
||
echo "Continue Handle Worker\n"; | ||
} | ||
|
||
public function onTick($timer_id, $params = null) { | ||
echo "Timer {$timer_id} running\n"; | ||
echo "Params: {$params}\n"; | ||
|
||
echo "Timer running\n"; | ||
echo "recv: {$params}\n"; | ||
|
||
var_dump($this->test); | ||
} | ||
} | ||
|
||
$server = new Server(); |