Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Asynchronous programming with PHP and OpenSwoole - ConFoo 2023

Asynchronous programming with PHP and OpenSwoole - ConFoo 2023

OpenSwoole is a PHP extension that allows to build high-performance, async multi-tasking web services with PHP. It offers a very lightweight Coroutine API to that allows to create thousands coroutines within one Linux process.

It offers a range of multi-threaded I/O modules (HTTP Server, WebSockets, TaskWorkers, Process Pools) out of the box and support for popular PHP clients like PDO and CURL.

URL: https://confoo.ca/en/2023
---
Info about OpenSwoole: https://openswoole.com/

Johan Janssens

February 23, 2023
Tweet

More Decks by Johan Janssens

Other Decks in Programming

Transcript

  1. 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”.
  2. 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.
  3. 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.
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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.
  11. 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.