Slide 1

Slide 1 text

http://www.joomlatools.com/developer @joomlatools Asynchronous programming with PHP and OpenSwoole Fast, faster … OpenSwoole!

Slide 2

Slide 2 text

Johan Janssens
 Web Developer and OpenSwoole Contributor / Evangelist @johanjanssens johanjanssens johanjanssens

Slide 3

Slide 3 text

1995, PHP/FI is announced….

Slide 4

Slide 4 text

1 PHP development began in 1994 when Rasmus Lerdorf wrote several Common Gateway Interface (CGI) programs in C, which he used to maintain his personal homepage. He extended them to work with web forms and to communicate with databases, and called this implementation "Personal Home Page/ Forms Interpreter" or PHP/FI. 
 
 PHP/FI could be used to build simple, dynamic web applications. To accelerate bug reporting and improve the code, Lerdorf initially announced the release of PHP/FI as "Personal Home Page Tools (PHP Tools) version 1.0”.

Slide 5

Slide 5 text

2 Early PHP was not intended to be a new programming language, and grew organically, with Lerdorf noting in retrospect: "I don't know how to stop it, there was never any intent to write a programming language [...] I have absolutely no idea how to write a programming language, I just kept adding the next logical step on the way." A development team began to form and, after months of work and beta testing, officially released PHP/FI 2 in November 1997.

Slide 6

Slide 6 text

3 Zeev Suraski and Andi Gutmans rewrote the parser in 1997 and formed the base of PHP 3, changing the language's name to the recursive acronym PHP: Hypertext Preprocessor Afterwards, public testing of PHP 3 began, and the official launch came in June 1998. They then started a new rewrite of PHP's core, producing the Zend Engine in 1999 They also founded Zend Technologies in Ramat Gan, Israel. On May 22, 2000, PHP 4, powered by the Zend Engine 1.0, was released.

Slide 7

Slide 7 text

4 Selecting data from an MySQL database and rendering HTML 4 using PHP.

Slide 8

Slide 8 text

What if PHP ….

Slide 9

Slide 9 text

The need for speed!

Slide 10

Slide 10 text

http://www.joomlatools.com/developer @joomlatools Meet OpenSwoole! Blazing fast event-driven asynchronous PHP

Slide 11

Slide 11 text

5 OpenSwoole is a CLI PHP extension that allows you to build high-performance, async multi-tasking webservices and applications using an easy to use Coroutine API. Compared with other async programming frameworks or software such as Node.js, Go, Python, ... OpenSwoole is a complete async solution that has built-in support for async programming via coroutines. It offers a range of multi-threaded I/O modules (HTTP Server, WebSockets, GRPC TaskWorkers, Process Pools) out of the box and support for popular PHP clients like PDO for MySQL, and CURL. You can find more info at https://openswoole.com

Slide 12

Slide 12 text

6 1. PHP 7.4 / 8.1 and 8.2, with PSR support 2. PHP Extension and PHP Server 3. TCP/UDP, HTTP, WebSocket, MQTT, GRPC support 4. Asynchronous and none blocking IO 5. Asynchronous PHP using a Coroutine API 6. Production Ready (Swoole 1.0 released in 2012) At a glance

Slide 13

Slide 13 text

http://www.joomlatools.com/developer @joomlatools Concepts

Slide 14

Slide 14 text

7 Synchronous vs Asynchronous

Slide 15

Slide 15 text

8 Event Loop and Reactor Mechanisms: epoll(), kqueue(), select()

Slide 16

Slide 16 text

9 Preemptive Multitasking Preemptive multitasking also known as time-shared multitasking is a type of multitasking that allows computer programs to share operating systems and underlying hardware resources. 
 
 It works on a time sharing feature, where each process may be allocated equal shares of computing resources. However, depending on a task's criticality and priority, additional time may be allocated. To prevent a program from taking control of computing resources, preemptive multitasking restricts the program to limited time slices. 5msec

Slide 17

Slide 17 text

10 Coroutine A coroutine is a code block that does allow execution to be suspended and resumed, 
 preemptively by the Coroutine Scheduler. 
 
 Coroutines are stackless: they suspend execution by returning to the caller and the data that is required to resume execution is stored separately from the stack. A Fiber is a code block that maintains its own stack (variables and state), that can be started, suspended, or terminated cooperatively by the main code and the Fiber Fiber Compared with yield/generator and fibres coroutines, yield is not ideal for I/O switching. OpenSwoole is more convenient because it handles a lot of the I/O switching for you

Slide 18

Slide 18 text

OpenSwoole vs PHP-FPM

Slide 19

Slide 19 text

11 1. Receive the request 
 2. Load and compile PHP files and codes 
 3. Initialise the context objects and variables 
 4. Execute functions 
 5. Send the response 
 6. Recycle the resources The life cycle of a HTTP request in PHP-FPM

Slide 20

Slide 20 text

12 1. Load and compile PHP files and codes 
 2. Initialise the context objects and variables 3. Receive the request 
 4. Execute functions 
 5. Send the response 
 6. Recycle the resources The life cycle of a HTTP request in OpenSwoole

Slide 21

Slide 21 text

http://www.joomlatools.com/developer @joomlatools Getting started

Slide 22

Slide 22 text

13 - 1. Install PHP, OpenSwoole and OpenSwoole Core Library

Slide 23

Slide 23 text

http://www.joomlatools.com/developer @joomlatools Setting up a HTTP Server

Slide 24

Slide 24 text

14 - 1. OpenSwoole HTTP WebServer

Slide 25

Slide 25 text

15 OpenSwoole Server Modes Multi process mode, the business logic is running in the child processes, this is the default running mode of a server Allows to define how connections are dispatched, by default dispatches the connection to the worker according to the ID number of connection (file descriptor). The data from the same connection will be handled by the same worker process. Reactor based mode, the business logic is running in the reactor thread, similar to other servers like Nginx and Node.js 
 No delivery of connections, just a range of identical processes that handle a request. Once the server receives a request, the processing and data is returned from the same thread.

Slide 26

Slide 26 text

16 OpenSwoole Hot Code Reloading

Slide 27

Slide 27 text

http://www.joomlatools.com/developer @joomlatools Asynchronous wit h the Coroutine API

Slide 28

Slide 28 text

17 Synchronous IO

Slide 29

Slide 29 text

18 Coroutine Async IO

Slide 30

Slide 30 text

19 Coroutine syncing with WaitGroup

Slide 31

Slide 31 text

http://www.joomlatools.com/developer @joomlatools Handling long running tasks with Task Workers

Slide 32

Slide 32 text

20 A task worker example

Slide 33

Slide 33 text

http://www.joomlatools.com/developer @joomlatools Preventing memory leaks

Slide 34

Slide 34 text

21 Memory leak prevention and management A worker process is restarted to avoid memory leak when receiving max_request + rand(0, max_request_grace) requests. The default value of max_request_grace is (max_request / 2); If the max_request is set to some number, the worker process will exit and release all the memory and resource occupied by this process after receiving the max_request request. And then, the manager will respawn a new worker process to replace it.

Slide 35

Slide 35 text

http://www.joomlatools.com/developer @joomlatools What about existing PHP applications?

Slide 36

Slide 36 text

22 A Swoole FastCGI client https://github.com/hollodotme/fast-cgi-client

Slide 37

Slide 37 text

Lies, tests and benchmarks

Slide 38

Slide 38 text

Swoole Performance vs PHP frameworks https://www.techempower.com/benchmarks

Slide 39

Slide 39 text

Swoole Performance vs Nodejs and Go https://www.techempower.com/benchmarks

Slide 40

Slide 40 text

It’s demo time