Home >php教程 >php手册 >swoole 1.7.3发布,增加PHP多进程管理模块

swoole 1.7.3发布,增加PHP多进程管理模块

WBOY
WBOYOriginal
2016-06-06 20:12:451284browse

PHP的异步并行扩展swoole,最近增加了多进程管理模块swoole_process,用来替代PHP的pcntl扩展。 PHP自带的pcntl,存在很多不足,如: pcntl无法用在fpm/apache中 pcntl没有提供进程间通信的功能 pcntl不支持重定向标准输入和输出 pcntl只提供了fork这样原始

PHP的异步并行扩展swoole,最近增加了多进程管理模块swoole_process,用来替代PHP的pcntl扩展。

PHP自带的pcntl,存在很多不足,如:

  • pcntl无法用在fpm/apache中
  • pcntl没有提供进程间通信的功能
  • pcntl不支持重定向标准输入和输出
  • pcntl只提供了fork这样原始的接口,容易使用错误

swoole_process提供了比pcntl更强大的功能,更易用的面向对象风格API,使PHP在多进程编程方面更加轻松。

swoole_process提供的功能特性:

  • swoole_process提供了基于unixsock的进程间通信,使用很简单只需调用recv/send即可
  • swoole_process支持重定向标准输入和输出,在子进程内echo不会打印屏幕,而是写入管道,读键盘输入可以重定向为管道读取数据
  • swoole_process允许用于fpm/apache的Web请求中
  • 配合swoole_event模块,创建的PHP子进程可以异步的事件驱动模式
  • swoole_process提供了exec接口,创建的进程可以执行其他程序,与原PHP父进程之间可以方便的通信
  • swoole_process可以安全地用于fpm/apache环境下

代码示例:

Php代码

  1. $worker_num?=?8;
  2. for($i?=?0;?$i?$worker_num;?$i++)
  3. {
  4. ????$process?=?new?swoole_process(‘callback_function’,?true);
  5. ????$pid?=?$process->start();
  6. ????$workers[$pid]?=?$process;
  7. }
  8. foreach($workers?as?$pid?=>?$process)
  9. {
  10. ????$process->write(“hello?worker[$pid]\n”);
  11. ????echo?”From?Worker:?”.$process->read();
  12. }
  13. for($i?=?0;?$i?$worker_num;?$i++)
  14. {
  15. ????$ret?=?swoole_process::wait();
  16. ????$pid?=?$ret['pid'];
  17. ????unset($workers[$pid]);
  18. ????echo?”Worker?Exit,?PID=”.$pid.PHP_EOL;
  19. }
  20. function?callback_function(swoole_process?$worker)
  21. {
  22. ????//echo?”Worker:?start.?PID=”.$worker->pid.”\n”;
  23. ????//recv?data?from?master
  24. ????$recv?=?$worker->read();
  25. ????echo?”From?Master:?$recv\n”;
  26. ????//send?data?to?master
  27. ????$worker->write(“hello?master\n”);
  28. ????sleep(2);
  29. ????$worker->exit(0);
  30. }

子进程事件驱动模式

Php代码

  1. function?callback_function_async(swoole_process?$worker)
  2. {
  3. ????//echo?”Worker:?start.?PID=”.$worker->pid.”\n”;
  4. ????//recv?data?from?master
  5. ????$GLOBALS['worker']?=?$worker;
  6. ????swoole_event_add($worker->pipe,?function($pipe)?{
  7. ????????$worker?=?$GLOBALS['worker'];
  8. ????????$recv?=?$worker->read();
  9. ????????echo?”From?Master:?$recv\n”;
  10. ????????//send?data?to?master
  11. ????????$worker->write(“hello?master\n”);
  12. ????????sleep(2);
  13. ????????$worker->exit(0);
  14. ????});
  15. }

PHP创建一个Python子进程,并与之通信

Php代码

  1. $process?=?new?swoole_process(‘pyhon_process’,?true);
  2. $pid?=?$process->start();
  3. function?pyhon_process(swoole_process?$worker)
  4. {
  5. ????$worker->exec(‘/usr/bin/python’,?array(“echo.py”));
  6. }
  7. $process->write(“hello?world\n”);
  8. echo?$process->read();
  9. $ret?=?swoole_process::wait();
  10. var_dump($ret);

Python程序echo.py

Python代码

  1. import?sys
  2. def?main():
  3. ????s?=?raw_input()
  4. ????print?”Python:”?+?s
  5. main()
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn