PHP asynchronous high concurrency extension Swoole

Source: Internet
Author: User
The advantages of php are not mentioned. Swole may be familiar to some people, which is also a major innovation of php.

The advantages of php are not mentioned. Swole may be familiar to some people, which is also a major innovation of php.

Next we will introduce it.

GitHub address: https://github.com/matyhtf/swoole

Official website: http://www.swoole.com

Pecl address: http://pecl.php.net/package/swoole

The swoole project released version 1.6.10 before the Spring Festival.

Kernel Update:

  • Enabled by defaultasync_mysql (the async_mysql feature is Enabled by default in version 5.4)

  • UDP send no require from_id (udp send does not contain from_id)

  • Add last_time and connect_time to tcp connection_info (link information adds attributes: connection time and last message arrival time)

  • Add tcp heartbeat mechanic (built-in tcp heartbeat detection module)

  • Rebuild data_buffer (restructured data_buffer)

Client module:

  • Swoole_client on: Receive and on: Close no need to call recv/close (the corresponding callback is not required for Receive and Close)

Php method change:

  • Add heartbeart () method (swoole_server_heartbeart () or $ serv-> heartbeat () (manual heartbeart detection)

Configuration change:

  • Add heartbeat_idle_time (maximum idle time of heartbeat)

  • Add heartbeat_check_interval (timed heartbeat detection time)

Swoole introduction:

Swoole is a php c extension that can be used to develop PHP's high-performance, high-concurrency TCP/UDP Server. The network I/O of Swoole is based on the multi-thread epoll/kqueue event loop, which is completely asynchronous and non-blocking. The business logic part can be fully asynchronous callback execution like node. js, or run in multi-process synchronous blocking mode. This ensures that the Server can cope with high concurrency and a large number of TCP connections. It also ensures that the business code can still be simply written.

Server sample code:

Php code

 
  1. $ Serv = new swoole_server ("maid", 9501 );

  2. $ Serv-> set (array (

  3. 'Worker _ num' => 2,

  4. ));

  5. $ Serv-> on ('connect ', function ($ serv, $ fd ){

  6. Echo "Client: Connect. n ";

  7. });

  8. $ Serv-> on ('receive ', function ($ serv, $ fd, $ from_id, $ data ){

  9. $ Serv-> send ($ fd, 'swoole: '. $ data );

  10. $ Serv-> close ($ fd );

  11. });

  12. $ Serv-> on ('close', function ($ serv, $ fd ){

  13. Echo "Client: Close. n ";

  14. });

  15. $ Serv-> start ();

  16. ?>

  17. Php server. php // Run

  18. Telnet 127.0.0.1 9501 // connect to the server

  19. Hello (send)

  20. Swoole: hello (receive)

Test it on your own. Very useful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.