Asynchronous Programming in PHP Lochemem Bruno Michael

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

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

$whoami I PHP/JS/C++ enthusiast

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

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

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

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

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

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

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)

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

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

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

Conventional HTTP configuration with PHP I Traditional LAMP stack

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

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

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

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

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

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

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

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

What are the requirements? I An event loop

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

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

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

All the way asynchronous Pretty popular

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

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

The original React

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

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

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

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

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

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

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

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

How is data conveyed in an event-driven system?

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

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

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

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)

What about data propagation and action chains?

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

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

How about something practical?

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

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

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

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

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

A neat microframework built atop ReactPHP

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

Symfony & React

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)

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

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

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

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

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

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

ReactPHP has a vibrant ecosystem Check out the ReactPHP wiki

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

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

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

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

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

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

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

Thank you