2.0.1-Alpha

Swoole-2.0 �ṩ��PHPԭ��Э�̵�������PHP������԰���ͬ����ʽ��д���ײ�����ʹ���첽IO������������IO��ɺ��Զ��л�PHP��������ջ��

����Э�̲�����PHP��Yield/Generator�﷨��ʵ����������ͬ�����룬�첽IO��Swoole-2.0����Swoole-1.0�����ִ����ԣ�ͬʱ֧��ͬ���������첽�������ص���Э�� 3 �� IO ģ�͡�

Э�̿������Ϊ�û�̬�̣߳�ͨ��Э����������ռ�������л�������ڲ���ϵͳ���̻����̣߳�Э�����еIJ������������û�̬��ɣ��������л������ĸ��͡�Swoole����Ϊÿһ�����󴴽���Ӧ��Э�̣�����IO��״̬������ص���Э�̡�

Swoole-2.0����Э�̵����ƣ�

  1. �����߿���ʹ���ͳ��PHPͬ�����뷽ʽ����ʵ���첽IO����ȥʹ��Node.js��Swoole1.0��д�첽����ʱ����ʹ��Ƕ�׻ص�������Ч�ʵͣ��߼�����֮�󿪷�ά���������ѡ�����ʹ��Swoole-2.0����Э�̿������ɱ�д�첽���򣬳��׸��Callback Hell��

  2. ����Swoole-2.0���ڵײ��װ��Э�̣����ԶԱ�������PHPЭ�̿�ܣ���TSF��Zan�������߲���Ҫ����ʹ��yield�ؼ�������ʶһ��Э��IO�л����������Բ�����Ҫ��yield�����������������Լ���ÿһ���ĵ��ö��޸�Ϊyield���⼫�������˿���Ч�ʡ�

  3. Swoole-2.0����֧�ֳ�������TCP��UDP��HTTP��Redis��MySQLЭ�̿ͻ���֮�⣬��֧�ֲ���Client������ͬʱ����ִ�ж����ͬ���͵�Э�̿ͻ��˵��ã�ʱ��ΪMax(��ʱ�Client��ʱ��)����Go������Ҫʹ�÷dz����ӵ�goroutine+chan+select����ʵ�֡�Swoole-2.0�IJ�������ʹ�ø��Ӽ򵥡�

��Ҫ���ԣ�

Ҫ��:

��װ��ʽ:

Swoole-2.0��Ҫͨ�����--enable-coroutine�����������Э��������ʾ�����£�

phpize
./configure --with-php-config={path-to-php-config}  --enable-coroutine
make
make install

��ӱ��������swoole server���л���Э��ģʽ

ʹ��ʾ��

TCPЭ�̿ͻ���

$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
$client->connect('127.0.0.1', 9501, 0.5)
//����connect������Э���л�
$client->send("hello world\n");
echo $client->recv();
//����recv���ٴδ���Э���л�
$client->close();

HttpЭ�̿ͻ���

$cli = new Swoole\Coroutine\Http\Client('127.0.0.1', 80);
$cli->setHeaders([
    'Host' => "localhost",
    "User-Agent" => 'Chrome/49.0.2587.3',
    'Accept' => 'text/html,application/xhtml+xml,application/xml',
    'Accept-Encoding' => 'gzip',
]);
$cli->set([ 'timeout' => 1]);
$cli->get('/index.php');
echo $cli->body;
$cli->close();

RedisЭ�̿ͻ���

$redis = new Swoole\Coroutine\Redis();
$redis->connect('127.0.0.1', 6379);
$val = $redis->get('key');

MySQLЭ�̿ͻ���

$swoole_mysql = new Swoole\Coroutine\MySQL();
$swoole_mysql->connect(['host' => '127.0.0.1',
    'user' => 'user',
    'password' => 'pass', 
    'database' => 'test',
]);
$res = $swoole_mysql->query('select sleep(1)');

��������

$tcpclient = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
$tcpclient->connect('127.0.0.1', 9501, 0.5)
$tcpclient->send("hello world\n");

$redis = new Swoole\Coroutine\Redis();
$redis->connect('127.0.0.1', 6379);
$redis->setDefer();
$redis->get('key');

$mysql = new Swoole\Coroutine\MySQL();
$mysql->connect(['host' => '127.0.0.1',
    'user' => 'user',
    'password' => 'pass',
    'database' => 'test',
]);
$mysql->setDefer();
$mysql->query('select sleep(1)');

$httpclient = new Swoole\Coroutine\Http\Client('0.0.0.0', 9599);
$httpclient->setHeaders(['Host' => "api.mp.qq.com"]);
$httpclient->set([ 'timeout' => 1]);
$httpclient->setDefer();
$httpclient->get('/');

$tcp_res  = $tcpclient->recv();
$redis_res = $redis->recv();
$mysql_res = $mysql->recv();
$http_res  = $httpclient->recv();

ͨ�������һ��ҵ����������Ҫ��һ��redis�����һ��mysql������ô����IO����������

redis����->redis�հ�->mysql����->mysql�հ�

������������IO��ʱ��͵��� redis����IOʱ�� + mysql����IOʱ�䡣

������Э�̰汾��Client������IO������������

redis����->mysql����->redis�հ�->mysql�հ�

������������IO��ʱ��ͽӽ��� MAX(redis����IOʱ��, mysql����IOʱ��)��

����֧�ֲ��������Client�У�

����Swoole\Coroutine\Client������Client��ʵ����defer���ԣ����������ӳ��հ���

ע������

  1. ȫ�ֱ�����Э��ʹ��ԭ�е��첽�߼�ͬ������������Э�̵��л�����ʽ�����ģ�������Э���л���ǰ���ܱ�֤ȫ�ֱ����Լ�static������һ���ԡ�
  2. ���������г����е���Э�̿ͻ��ˣ�
    • ��������__destruct()
    • ħ������__call()
  3. ��ǰ�汾ΪAlphaԤ���汾��������������������ʹ��

ʹ���°汾

��������Ⱥ 494412579