You've already forked qlg.tsgz.moe
addons
app_download_files
extend
hyhproject
mobile
oss
static
thinkphp
upload
vendor
5ini99
composer
oss-sdk
swoole
.github
benchmark
examples
async
atomic
channel
client
coroutine
eof
event
http
http2
ipv6
length
lock
memory
mmap
multicast
mysql
namespace
php
postgresql
process
alarm.php
async_master.php
client.php
client3.php
close.php
echo.py
exec.php
func_timeout.php
msgqueue.php
msgqueue2.php
msgqueue_client.php
msgqueue_pool.php
pool_socket.php
python.php
select.php
stdin_stdout.php
test.php
worker.php
redis
runtime
server
socket_coro
ssl
table
task
timer
udp
unixsock
websocket
buffer.php
c10k.php
channel.php
client2.php
db_pool.php
get_local_ip.php
hot_update_class.php
multi_port_server.php
mysql_proxy_server.php
proxy_sync.php
recv_1m_client.php
recv_file.php
redis_pool.php
reflection_test.php
ringqueue.php
send_1m_svr.php
sendfile_server.php
serialize.php
serialize2.php
server.c
server.php
server_hot_update_opcache.php
set_cpu_affinity.php
swoole_http_client.php
test.jpg
test_buffer.php
test_server.c
version.php
weather_server.php
include
src
tests
thirdparty
tools
travis
.gitignore
.gitmodules
.travis.yml
CMakeLists.txt
CREDITS
LICENSE
README.md
Version2.md
build.sh
clear.sh
config.m4
make.sh
package.xml
php7_wrapper.h
php_swoole.h
swoole.c
swoole_async.c
swoole_atomic.c
swoole_buffer.c
swoole_channel.c
swoole_channel_coro.cc
swoole_client.c
swoole_client_coro.c
swoole_config.h
swoole_coroutine.cc
swoole_coroutine.h
swoole_coroutine_util.c
swoole_event.c
swoole_http.h
swoole_http_client.c
swoole_http_client.h
swoole_http_client_coro.c
swoole_http_server.c
swoole_http_v2_client.c
swoole_http_v2_client.h
swoole_http_v2_client_coro.c
swoole_http_v2_server.c
swoole_lock.c
swoole_memory_pool.c
swoole_mmap.c
swoole_msgqueue.c
swoole_mysql.c
swoole_mysql.h
swoole_mysql_coro.c
swoole_postgresql_coro.c
swoole_postgresql_coro.h
swoole_process.c
swoole_process_pool.c
swoole_redis.c
swoole_redis_coro.c
swoole_redis_server.c
swoole_ringqueue.c
swoole_runtime.cc
swoole_serialize.c
swoole_serialize.h
swoole_server.c
swoole_server_port.c
swoole_socket_coro.c
swoole_table.c
swoole_timer.c
swoole_trace.c
swoole_websocket_server.c
wechat
.htaccess
autoload.php
wxtmp
.gitignore
.htaccess
.user.ini
404.html
H5436787D.wgt
admin.php
app-release.apk
app_download.html
cash.lock
demo.php
get_version.php
get_version_new.php
index.html
index.php
reg.lock
robots.txt
107 lines
2.7 KiB
PHP
Executable File
107 lines
2.7 KiB
PHP
Executable File
<?php
|
|
$redirect_stdout = false;
|
|
$workers = [];
|
|
$worker_num = 1;
|
|
|
|
//swoole_process::daemon(0, 1);
|
|
for($i = 0; $i < $worker_num; $i++)
|
|
{
|
|
$process = new swoole_process('child_async', $redirect_stdout);
|
|
$process->id = $i;
|
|
$pid = $process->start();
|
|
$workers[$pid] = $process;
|
|
//echo "Master: new worker, PID=".$pid."\n";
|
|
}
|
|
|
|
master_async($workers);
|
|
//master_sync($workers);
|
|
|
|
//异步主进程
|
|
function master_async($workers)
|
|
{
|
|
swoole_process::signal(SIGCHLD, function ($signo) use (&$workers) {
|
|
while(1)
|
|
{
|
|
$ret = swoole_process::wait(false);
|
|
if ($ret)
|
|
{
|
|
$pid = $ret['pid'];
|
|
$child_process = $workers[$pid];
|
|
//unset($workers[$pid]);
|
|
echo "Worker Exit, kill_signal={$ret['signal']} PID=" . $pid . PHP_EOL;
|
|
$new_pid = $child_process->start();
|
|
$workers[$new_pid] = $child_process;
|
|
unset($workers[$pid]);
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @var $process swoole_process
|
|
*/
|
|
foreach($workers as $pid => $process)
|
|
{
|
|
swoole_event_add($process->pipe, function($pipe) use ($process) {
|
|
$recv = $process->read();
|
|
if ($recv) echo "From Worker: " . $recv;
|
|
$process->write("HELLO worker {$process->pid}\n");
|
|
});
|
|
$process->write("hello worker[$pid]\n");
|
|
}
|
|
}
|
|
|
|
//同步主进程
|
|
function master_sync($workers)
|
|
{
|
|
foreach($workers as $pid => $process)
|
|
{
|
|
$process->write("hello worker[$pid]\n");
|
|
echo "From Worker: ".$process->read();
|
|
}
|
|
}
|
|
|
|
function child_sync(swoole_process $worker)
|
|
{
|
|
//echo "Worker: start. PID=".$worker->pid."\n";
|
|
//recv data from master
|
|
$recv = $worker->read();
|
|
|
|
echo "From Master: $recv\n";
|
|
|
|
//send data to master
|
|
$worker->write("hello master\n");
|
|
|
|
sleep(2);
|
|
$worker->exit(0);
|
|
}
|
|
|
|
function child_async(swoole_process $worker)
|
|
{
|
|
//echo "Worker: start. PID=".$worker->pid."\n";
|
|
//recv data from master
|
|
$GLOBALS['worker'] = $worker;
|
|
global $argv;
|
|
$worker->name("{$argv[0]}: worker #".$worker->id);
|
|
|
|
swoole_process::signal(SIGTERM, function($signal_num) use ($worker) {
|
|
echo "signal call = $signal_num, #{$worker->pid}\n";
|
|
});
|
|
|
|
// swoole_timer_tick(2000, function () use ($worker)
|
|
// {
|
|
// if (rand(1, 3) % 2) {
|
|
// $worker->write("hello master {$worker->pid}\n");
|
|
// }
|
|
// });
|
|
|
|
swoole_event_add($worker->pipe, function($pipe) use($worker) {
|
|
$recv = $worker->read();
|
|
echo "From Master: $recv\n";
|
|
//$worker->write("hello master\n");
|
|
});
|
|
}
|