Asynchronous Programming in PHP

A presentation at PHP Conference Japan 2021 in October 2021 in by Lochemem Bruno Michael

Slide 1

Slide 1

Asynchronous Programming in PHP Lochemem Bruno Michael

Slide 2

Slide 2

Agenda I I I I I I I I I I Introduction The rigors of I/O Asynchrony The asynchronous PHP landscape The event loop Streams Promises Sockets HTTP Servers Command Line applications

Slide 3

Slide 3

Lochemem Bruno Michael https://chemem.site @agiroLoki @ace411

Slide 4

Slide 4

$whoami I PHP/JS/C++ enthusiast

Slide 5

Slide 5

$whoami I PHP/JS/C++ enthusiast I Functional Programming aficionado

Slide 6

Slide 6

$whoami I PHP/JS/C++ enthusiast I Functional Programming aficionado I Maintainer of several packages and PHP extensions

Slide 7

Slide 7

$whoami I I I I PHP/JS/C++ enthusiast Functional Programming aficionado Maintainer of several packages and PHP extensions Author

Slide 8

Slide 8

$whoami I I I I I PHP/JS/C++ enthusiast Functional Programming aficionado Maintainer of several packages and PHP extensions Author Hooper

Slide 9

Slide 9

$whoami I I I I I I PHP/JS/C++ enthusiast Functional Programming aficionado Maintainer of several packages and PHP extensions Author Hooper Gamer

Slide 10

Slide 10

A lot of usable software is a combination of Input-Output (I/O) operations

Slide 11

Slide 11

I/O everywhere? I I I I I Filesystem interactions Database interactions Reading from Standard Input (STDIO) Writing to Standard Output (STDOUT) API calls (REST, SOAP)

Slide 12

Slide 12

I/O is slow Access type L1 cache reference Send packet CA->Holland->CA Latency (ns) 0.5 150,000,000 Fun fact If you multiply the durations by a billion, the former’s latency is the equivalent of one heartbeat, and the latter’s approximates an entire Bachelor’s degree program. Source: Latency numbers every programmer should know

Slide 13

Slide 13

Traditional PHP is, despite recent improvements, not immune to this problem

Slide 14

Slide 14

Blocking I/O Galore I Sequential execution of function calls I Multiple idle periods between successive executions I Direct result-to-variable binding

Slide 15

Slide 15

Conventional HTTP configuration with PHP I Traditional LAMP stack

Slide 16

Slide 16

Conventional HTTP configuration with PHP I Traditional LAMP stack I Often tuned up with PHP-FPM

Slide 17

Slide 17

Conventional HTTP configuration with PHP I Traditional LAMP stack I Often tuned up with PHP-FPM I Still works!

Slide 18

Slide 18

I/O’s only gotten more arduous I Live data

Slide 19

Slide 19

I/O’s only gotten more arduous I Live data I Server-Sent Events (SSE)

Slide 20

Slide 20

I/O’s only gotten more arduous I Live data I Server-Sent Events (SSE) I Robust HTTP APIs

Slide 21

Slide 21

I/O’s only gotten more arduous I I I I Live data Server-Sent Events (SSE) Robust HTTP APIs & more

Slide 22

Slide 22

Asynchrony is a potent answer to I/O-related problems

Slide 23

Slide 23

So, what is it? The ability to run multiple processes, independent of main program flow - by interleaving them in a single execution thread.

Slide 24

Slide 24

What are the requirements? I An event loop

Slide 25

Slide 25

What are the requirements? I An event loop I A proxy mechanism for handling undetermined values

Slide 26

Slide 26

What are the requirements? I An event loop I A proxy mechanism for handling undetermined values I A lot of un-buffered data

Slide 27

Slide 27

What are the requirements? I I I I An event loop A proxy mechanism for handling undetermined values A lot of un-buffered data A single-threaded runtime

Slide 28

Slide 28

All the way asynchronous Pretty popular

Slide 29

Slide 29

But PHP is well-suited to the needs of asynchrony despite not offering it out-of-the-box

Slide 30

Slide 30

The asynchronous PHP landscape Tool Distribution ReactPHP Composer Amp Composer Swoole PECL Resemblances Node.JS Go, Node.JS Go, Node.JS

Slide 31

Slide 31

The original React

Slide 32

Slide 32

What is React? A suite of packages - based on the Reactor pattern - intended to enable event-driven programming in PHP. I Event loop

Slide 33

Slide 33

What is React? A suite of packages - based on the Reactor pattern - intended to enable event-driven programming in PHP. I Event loop I Stream abstraction

Slide 34

Slide 34

What is React? A suite of packages - based on the Reactor pattern - intended to enable event-driven programming in PHP. I Event loop I Stream abstraction I HTTP client and server

Slide 35

Slide 35

What is React? A suite of packages - based on the Reactor pattern - intended to enable event-driven programming in PHP. I Event loop I Stream abstraction I HTTP client and server I Child processes

Slide 36

Slide 36

At the core of many event-driven systems is the event loop.

Slide 37

Slide 37

The event loop I A low-level dispatcher I A quasi-scheduler I Monitors an execution context for events I Dispatches handler to event I Can be written in PHP

Slide 38

Slide 38

The event loop I Listens for events and dispatches actions to process them I Renders everything in its context non-blocking I Runs until the point of event completion or stoppage

Slide 39

Slide 39

Want more power? Plug in a suitable extension! I ext-ev I ext-uv I ext-event

Slide 40

Slide 40

How is data conveyed in an event-driven system?

Slide 41

Slide 41

How is data conveyed in an event-driven system? Usually, as a stream…

Slide 42

Slide 42

Streams I Typically un-buffered sequences of data I Can be connected in pipelines I Readable (like STDIN)

Slide 43

Slide 43

Streams I Typically un-buffered sequences of data I Can be connected in pipelines I Writable (like STDOUT)

Slide 44

Slide 44

Streams I Typically un-buffered sequences of data I Can be connected in pipelines I Duplex (like TCP/IP or file in read/write mode)

Slide 45

Slide 45

What about data propagation and action chains?

Slide 46

Slide 46

What about data propagation and action chains? How about promises?

Slide 47

Slide 47

Promises I Placeholders for values yet to be computed I Possess two tracks (resolve, reject) I Algebraic structures

Slide 48

Slide 48

How about something practical?

Slide 49

Slide 49

How about something practical? Like a simple socket-powered chat?

Slide 50

Slide 50

Sockets I Suitable for network communications I Useful in client-server setups I Data typically conveys as a duplex stream telnet <addresss> <port>

Slide 51

Slide 51

Trying to set up an HTTP server? PHP is all you need.

Slide 52

Slide 52

Server I Pure PHP I PSR-compliant software I Reliably fast I Multiple integrations with existent PHP packages

Slide 53

Slide 53

Client I Promise-driven HTTP client I Akin to JavaScript’s fetch API I Easy to use I Also PSR-compliant

Slide 54

Slide 54

A neat microframework built atop ReactPHP

Slide 55

Slide 55

framework-x I Created and maintained by clue.engineering I Process all kinds of data (.json, .csv, .xml etc) I Run in any environment I Go from RAD to production in minutes

Slide 56

Slide 56

Symfony & React

Slide 57

Slide 57

DriftPHP I Created and maintained by Marc Morera I Non-blocking Symfony kernel I Promise-driven controllers I Asynchronous components (command bus, file watcher etc)

Slide 58

Slide 58

So, you want to run blocking code in an event-driven system?

Slide 59

Slide 59

So, you want to run blocking code in an event-driven system? This is conventionally a no-no but…

Slide 60

Slide 60

So, you want to run blocking code in an event-driven system? This is conventionally a no-no but…

Slide 61

Slide 61

Synchronous to asynchronous I Works on many non-blocking PHP functions I Utilizes child-process I/O I Supports FP and traditional OO approaches

Slide 62

Slide 62

How about applications that run in the console? Also considered user-facing software

Slide 63

Slide 63

Shells I Text input-driven I Read->Evaluate->Print>Loop I Can benefit from the potency of non-blocking I/O

Slide 64

Slide 64

ReactPHP has a vibrant ecosystem Check out the ReactPHP wiki

Slide 65

Slide 65

ReactPHP has a vibrant ecosystem Check out the ReactPHP wiki I It is growing

Slide 66

Slide 66

ReactPHP has a vibrant ecosystem Check out the ReactPHP wiki I It is growing I Package updates are regularly released

Slide 67

Slide 67

Additional Material I I I I ReactPHP documentation Asynchronous Programming in PHP Learning Event-Driven PHP with ReactPHP Entries in Sergey Zhuk’s blog

Slide 68

Slide 68

Please give asynchronous PHP a try. You likely won’t regret it!

Slide 69

Slide 69

Please give asynchronous PHP a try. You likely won’t regret it! I Write a simple REST API

Slide 70

Slide 70

Please give asynchronous PHP a try. You likely won’t regret it! I Write a simple REST API I Write a simple shell

Slide 71

Slide 71

Please give asynchronous PHP a try. You likely won’t regret it! I Write a simple REST API I Write a simple shell I Write a basic asynchronous I/O script

Slide 72

Slide 72

Thank you