Skip to content

Instantly share code, notes, and snippets.

@timglabisch
Created August 3, 2017 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timglabisch/c2f56b3b0232bfb38130c85f3034c6bc to your computer and use it in GitHub Desktop.
Save timglabisch/c2f56b3b0232bfb38130c85f3034c6bc to your computer and use it in GitHub Desktop.
<?php
use Swoole\Http\Server;
$serv = new swoole_http_server('0.0.0.0', 9501);
$serv->set(array(
'task_worker_num' => 10,
'task_max_request' => 1
));
$serv->on('request', function ($request, $response) use ($serv) {
$task_id = $serv->task("Async");
$response->header('Content-Type', 'text/html; charset=utf-8');
$response->end('<h1>Hello Swoole. #' . rand(1000, 9999) . '</h1>');
});
$serv->on('Start', function (Server $serv) {
sleep(1);
$conn_list = $serv->connection_list(0, 100);
swoole_process::signal(SIGQUIT, function($signo) use ($serv) {
echo "QUIT ON MASTER: $signo\n";
foreach (range(0, 10) as $worker_id) {
$serv->task($worker_id);
}
swoole_process::wait();
});
});
$serv->on('Task', function (Server $serv, $task_id, $from_id, $data) {
require __DIR__.'/foo.php';
echo "start work\n";
foreach (range(0, 100) as $i) {
echo "try to finish my work";
sleep(1);
}
// sleep(10);
echo "finished {$GLOBALS['worker_id']} work\n";
$serv->finish("$data -> OK");
});
$serv->on('workerStart', function(Server $serv, $worker_id) {
$GLOBALS['worker_id'] = $worker_id;
echo "Worker Start\n";
});
$serv->on('Finish', function ($serv, $task_id, $data) {
echo "AsyncTask[$task_id] Finish: $data".PHP_EOL;
});
$serv->start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment