php怎么实现多线程
PHP实现的多进程控制demo示例本文实例讲述了PHP实现的多进程控制。分享给大家供大家参考,具体如下:
自己写了个多进程控制的框架代码,留着备查
|
declare (ticks=1); function sigHandler( $signal ) { echo "a child exited\n" ; } pcntl_signal(SIGCHLD, sigHandler, false); echo "this is " . posix_getpid() . PHP_EOL; for ( $i =0; $i <3; $i ++) { $pid = pcntl_fork(); if ( $pid == -1) { echo 'fork failed ' . PHP_EOL; } else if ( $pid ) { } else { $pid = posix_getpid(); echo 'child ' . $pid . ' ' . time() . PHP_EOL; sleep(rand(2,5)); echo 'child ' . $pid . ' done ' . time() . PHP_EOL; exit (0); } } do { $pid = pcntl_wait( $status ); echo 'child quit ' . $pid . PHP_EOL; } while ( $pid > 0); echo 'parent done' . PHP_EOL; |
希望本文所述对大家PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/zhaozhi406/article/details/14379905