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. http://www.joomlatools.com/developer @joomlatools
    Asynchronous programming
    with PHP and OpenSwoole
    Fast, faster … OpenSwoole!

    View Slide

  2. Johan Janssens

    Web Developer and OpenSwoole Contributor / Evangelist
    @johanjanssens
    johanjanssens
    johanjanssens

    View Slide

  3. 1995, PHP/FI is announced….

    View Slide

  4. 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”.

    View Slide

  5. 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.

    View Slide

  6. 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.

    View Slide

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

    View Slide

  8. What if PHP ….

    View Slide

  9. The need for speed!

    View Slide

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

    View Slide

  11. 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

    View Slide

  12. 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

    View Slide

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

    View Slide

  14. 7
    Synchronous vs Asynchronous

    View Slide

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

    View Slide

  16. 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

    View Slide

  17. 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

    View Slide

  18. OpenSwoole vs PHP-FPM

    View Slide

  19. 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

    View Slide

  20. 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  24. 14
    -
    1.
    OpenSwoole HTTP WebServer

    View Slide

  25. 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.

    View Slide

  26. 16
    OpenSwoole Hot Code Reloading

    View Slide

  27. http://www.joomlatools.com/developer @joomlatools
    Asynchronous wit
    h

    the Coroutine API

    View Slide

  28. 17
    Synchronous IO

    View Slide

  29. 18
    Coroutine Async IO

    View Slide

  30. 19
    Coroutine syncing with WaitGroup

    View Slide

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

    View Slide

  32. 20
    A task worker example

    View Slide

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

    View Slide

  34. 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.

    View Slide

  35. http://www.joomlatools.com/developer @joomlatools
    What about existing


    PHP applications?

    View Slide

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

    View Slide

  37. Lies, tests and benchmarks

    View Slide

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

    View Slide

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

    View Slide

  40. It’s demo time

    View Slide