项目作者: deminy
项目描述 :
Learn Swoole by Examples
高级语言: Dockerfile
项目地址: git://github.com/deminy/swoole-by-examples.git
Swoole by Examples

The repository is to help developers to get familiar with Swoole through a
variety of examples. All the examples are fully functioning; they can be executed and verified using the Docker images
provided.
NOTE: I’m adding examples for latest versions of Swoole, so please be patient.
Setup the Development Environment
We use Docker to setup our development environment. Other than Docker, you don’t need to install any other software to
run and test the examples: you don’t need to have PHP, Swoole, Composer, or some other software installed locally.
We use the official Docker image of Swoole to run the examples. There are
tens of examples under repository swoole/docker-swoole shown how to use the
image. Please spend some time checking it first.
Before running the examples, please run command docker-compose up -d
under the root repository directory to start the
Docker containers. There are two containers used to run the examples:
- a server container where application servers are running.
- a client container where client-side scripts should be executed.
Both containers have the same PHP scripts in place, so most standalone scripts (e.g., most CSP programming examples) can be
executed from either container. Once the containers are running, you can use one of following commands to get a Bash shell
in the containers:
docker compose exec -ti server bash # Get a Bash shell in the server container.
docker compose exec -ti client bash # Get a Bash shell in the client container.
List of Examples
- CSP programming
- from blocking I/O to non-blocking I/O
- The blocking version can be found here.
- The non-blocking version of the same script can be found here. You can also check this script to see how the non-blocking version is executed in order.
- This example shows how the return statement is treated differently in Swoole. As you can see in the example, a function call could return a value back first before finishing its execution.
- coroutines
- channels
- defer
- runtime hooks
- locks
- deadlocks
- examples on deadlocks
- How to detect/handle deadlocks. In the following examples, we trigger deadlocks by yielding the execution of the only coroutine in the program.
- advanced topics
- server-side programming
- application servers
- resource pooling
- process pool
- connection pool
- network connection detection (dead network detection)
- task scheduling and handling
- timer
- There is a 2nd example included to show how to implement timer using coroutines only.
- To see how to setup cronjobs using the \Swoole\Timer class in an application server, please check integrated HTTP/1 server.
- cronjobs
- benchmark
- base mode vs multi-process mode
- advanced topics
- Rock Paper Scissors: implement the hand game Rock Paper Scissors using Swoole.
- How are different server events triggered?
- Server Combo (two different implementations)
- integrated HTTP/1 server: an HTTP/1 server that supports cron jobs and synchronous/asynchronous tasks.
- integrated WebSocket server: a WebSocket server that supports cron jobs and asynchronous tasks. This implementation use separate processes to handle cron jobs and task queues.
- mixed protocols
- DDoS protection: How to protect your Swoole-based application server from DDoS attacks.
- interruptible sleep: This example shows how to set up a cronjob in a web server, and allow the cronjob to execute at a last time when the server is shutting down.
- multiple ports listening
- client-side programming
- miscellaneous topics
- data management in Swoole: globals, persistence, and caching
- APCu caching: APCu caching in Swoole works the same way as in other PHP CLI applications. This example explains it in details.
- atomic counters
- multiprocessing
- wait and wakeup processes
- process pool
- pool creation and inter-process communication: Please check previous section
resource pooling
for details. - detach processes from a process pool