Swoole-PHP使用及示例

Swoole官网:https://wiki.swoole.com/wiki/page/p-coroutine_realization.html

在 Swoole 上运行 Symfony:https://www.chrisyue.com/run-symfony-on-swoole.html

Swoole is an event-driven asynchronous & concurrent networking communication framework with high performance written only in C for PHP.

Documenthttps://rawgit.com/tchiotludo/swoole-ide-helper/english/docs/index.html

IDE Helperhttps://github.com/swoole/ide-helper

中文文档http://wiki.swoole.com/

IRChttps://gitter.im/swoole/swoole-src

Event-based

The network layer in Swoole is event-based and takes full advantage of the underlaying epoll/kqueue implementation, making it really easy to serve thousands of connections.

Coroutine

Swoole 2.0 supports the built-in coroutine, and you can use fully synchronized code to implement asynchronous programs. PHP code without any additional keywords, the underlying automatic coroutine-scheduling.

Concurrent

On the request processing part, Swoole uses a multi-process model. Every process works as a worker. All business logic is executed in workers, synchronously.

With the synchronous logic execution, you can easily write large and robust applications and take advantage of almost all libraries available to the PHP community.

In-memory

Unlike traditional apache/php-fpm stuff, the memory allocated in Swoole will not be freed after a request, which can improve performance a lot.

Why Swoole?

Traditional PHP applications almost always run behind Apache/Nginx, without much control of the request. This brings several limitations:

  1. All memory will be freed after the request. All PHP code needs be re-compiled on every request. Even with opcache enabled, all opcode still needs to be re-executed.
  2. It is almost impossible to implement long connections and connection pooling techniques.
  3. Implementing asynchronous tasks requires third-party queue servers, such as rabbitmq and beanstalkd.
  4. Implementing realtime applications such as chatting servers requires third-party languages, such as nodejs, for example.

This is why Swoole appeared. Swoole extends the use cases of PHP, and brings all these possibilities to the PHP world. By using Swoole, you can build enhanced web applications with more control, real-time chatting servers, etc, more easily.

Requirements

  • Version-1: PHP 5.3.10 or later
  • Version-2: PHP 5.5.0 or later
  • Linux, OS X and basic Windows support (thanks to cygwin)
  • GCC 4.4 or later

Installation

  1. Install via pecl
  2. Install from source

Introduction

Swoole includes components for different purposes: Server, Task Worker, Timer, Event and Async IO. With these components, Swoole allows you to build many features.

Server

This is the most important part in Swoole. It provides the necessary infrastructure to build server applications. With Swoole server, you can build web servers, chat messaging servers, game servers and almost anything you want.

The following example shows a simple echo server.

Try to extend your server and implement what you want!

HTTP Server

WebSocket Server

Real async-mysql client

Real async-redis client

Async http Client

Async WebSocket Client

Multi-port and mixed protocol

Task Worker

Swoole brings you two types of workers: server workers and task workers. Server workers are for request handling, as demonstrated above. Task workers are for task execution. With task workers, we can execute our task asynchronously without blocking the server workers.

Task workers are mainly used for time-consuming tasks, such as sending password recovery emails, and ensure the main request returns as soon as possible.

The following example shows a simple server with task support.

Swoole also supports synchronous tasks. To use synchronous tasks, just simply replace $serv->task($data)with $serv->taskwait($data). Unlike task()taskwait() will wait for a task to complete before it returns its response.

Timer

Swoole has built-in millisecond timer support. By using the timer, it is easy to get a block of code executed periodically (really useful for managing interval tasks).

To demonstrate how the timer works, here is a small example:

In the example above, we first set the timer event handler to swoole_server to enable timer support. Then, we add two timers by calling bool swoole_server::addtimer($interval) once the server started. To handle multiple timers, we switch the $interval in registered handler and do what we want to do.

Event

Swoole’s I/O layer is event-based, which is very convenient to add your own file descriptor to Swoole’s main eventloop. With event support, you can also build fully asynchronous applications with Swoole.

To use events in Swoole, we can use swoole_event_set() to register event handler to sepecified file descriptor, once registered descriptors become readable or writeable, our registered handler will be invoked. Also, we can using bool swoole_event_del(int $fd); to remove registered file descriptor from eventloop.

The following are prototypes for the related functions:

The $fd parameter can be one of the following types:

  • unix file descriptor
  • stream resource created by stream_socket_client()/fsockopen()
  • sockets resources created by socket_create() in sockets extension (require compile swoole with –enable-sockets support)

The $read_callback and $write_callback are callbacks for corresponding read/write event.

The $flag is a mask to indicate what type of events we should get notified, can be SWOOLE_EVENT_READSWOOLE_EVENT_WRITE or SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE

Async IO

Swoole’s Async IO provides the ability to read/write files and lookup dns records asynchronously. The following are signatures for these functions:

Refer to API Reference for more detailed information about these functions.

Client

Swoole also provides a client component to build tcp/udp clients in both asynchronous and synchronous ways. Swoole uses the swoole_client class to expose all its functionalities.

synchronous blocking:

Asynchronous non-blocking:

The following methods are available in swoole_client:

Refer to API Reference for more detailed information about these functions.

Related Posts