当前位置: 代码迷 >> PHP >> 替PHP添加swoole异步并行扩展
  详细解决方案

替PHP添加swoole异步并行扩展

热度:97   发布时间:2016-04-28 17:05:23.0
为PHP添加swoole异步并行扩展

为PHP添加swoole异步并行扩展

?

?

1. 确认安装环境

  • 仅支持Linux操作系统,核心代码兼容FreeBSD,需要调整某些编译的参数和细节才能通过
  • Linux内核版本2.3.32以上
  • PHP5.3.10以上版本
  • gcc4.4以上版本。核心代码兼容clang,需要关闭CPU亲和设置特性
  • cmake2.4+,编译为libswoole.so作为C/C++库时,需要使用cmake

?

2. 安装与配置

2. 1安装swoole:可pecl直接在线安装,或下载releases版本的swoole编译安装

?2.11 pecl安装swoole扩展

pecl不可用的请确认php安装目录的bin目录已加入系统变量

?
1
# pecl install swoole?

?2.12编译安装swoole扩展

从此处下载:http://pecl.php.net/package/swoole

?
1
2
3
4
# cd swoole
# phpize
# ./configure
# make && make install

2.2配置php.ini

phpinfo()确认Configuration File (php.ini) Path

添加:

?
1
extension=swoole.so

重启Apache查看php扩展库

?
1
2
# service httpd restart
# php -m

确认swoole已添加

官方文档:http://wiki.swoole.com/wiki/index/prid-1

?

3.测试

server.php:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
????$serv = new swoole_server("0.0.0.0", 9501);
????$serv->on('connect', function ($serv, $fd){
????????echo "Client:Connect.\n";
????});
????$serv->on('receive', function ($serv, $fd, $from_id, $data) {
????????$serv->send($fd, 'Swoole: '.$data);
????});
????$serv->on('close', function ($serv, $fd) {
????????echo "Client: Close.\n";
????});
????$serv->start();
?>

client.php:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
????$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
????$client->on("connect", function($cli) {
????????$cli->send("hello world\n");
????});
????$client->on("receive", function($cli, $data){
????????echo "Receive: $data\n";
????});
????$client->on("error", function($cli){
????????echo "connect fail\n";
????});
????$client->on("close", function($cli){
????????echo "close\n";
????});
????$client->connect('127.0.0.1', 9501, 0.5);
?>

Test:

?测试Server:

???

????测试Client:

???

?

?

End.