SlideShare a Scribd company logo
A S Y N C H R O N O U S
P R O G R A M M I N G I N P H P
Z E N D C O N 2 0 1 6
W E L C O M E T O L A S V E G A S !
W H O A M I ?
W H Y A S Y N C H R O N O U S ?
H I G H T R A F F I C
R E A C T I V E A P P L I C AT I O N S
I N T E R N E T O F T H I N G S
S TAT E F U L P R O T O C O L S E R V E R S
• WebSocket
• MQTT
• AMQP
• IRC
• XMPP
• Telnet
• SSH
• RADIUS
• LDAP
• FTP
H T T P / 2
H O W D O Y O U D O A S Y N C ?
S Y S T E M L E V E L A S Y N C
• German
• Apache Spark
• AWS Lambda
• Stomp
• ZeroMQ
• AMQP
J O B S E R V E R S Q U E U I N G
C O D E L E V E L A S Y N C
• Forking and
Threading
• Asynchronous I/O
• Fork/Join
• Event Loop
S T R AT E G I E S M E T H O D O L O G I E S
F O R K I N G A N D T H R E A D I N G
• Available in PHP core since PHP 4.1
• Requires the use of shared memory
• Requires code to be written for management of
forks/threads
• Creates separate copies of code for each thread/
fork
F O R K I N G / T H R E A D I N G L I B R A R I E S
• PCNTL/POSIX — Forking and process management
• pthreads — Thread management
• Semaphore — Semaphore (locks) and shared memory
• Shared Memory — Shared memory
• Sync — Cross platform semaphore and shared
memory
A S Y N C H R O N O U S I / O
• Frees up the current process while I/O is
performed
• Executes code based on I/O events
• No copying of code for separate process
R E A L W O R L D P R O F I L E D ATA
C AT E G O RY S E G M E N T % T I M E
AV G C A L L S
( P E R T X N )
AV G
T I M E ( M S )
D ATA B A S E M Y S Q L A P P S S E L E C T 7 2 . 6 2 . 0 3 4 . 8
F U N C T I O N
A P I . H A N D L E R S . A P P S : A P P H A N
D L E R . R E N D E R _ R E S O U R C E
1 2 . 6 1 . 0 6 . 0 4
D ATA B A S E M Y S Q L A P P U S E R M A P S E L E C T 8 . 7 3 . 9 7 4 . 1 6
F U N C T I O N
P Y R A M I D . R O U T E R : R O U T E R . _ _
C A L L _ _
3 . 3 1 . 0 1 . 5 9
M Y S Q L O R G A N I Z AT I O N S
S E L E C T
2 . 2 0 . 7 1 . 5 2
A S Y N C H R O N O U S I / O L I B R A R I E S
• Streams via stream_select and socket_select
• eio — libeio
• ev — libev
• libevent — libevent
• event — libevent
F O R K / T H R E A D V S A S Y N C I / O
• Compute heavy
• Process isolation
• Non-Async library
• I/O processes
• Process/memory
optimization
F O R K / T H R E A D A S Y N C / I O
F O R K / J O I N PA R A L L E L I S M
S TA RT
P R O C E S S 1
P R O C E S S 2
P R O C E S S 3
E N DF O R K J O I N
F O R K / J O I N E X A M P L E
// JOIN

curl_multi_remove_handle($mh, $ch1);

curl_multi_remove_handle($mh, $ch2);

curl_multi_close($mh);

$resp = array(curl_multi_getcontent($ch1),
curl_multi_getcontent($ch2));
// FORK

$active = null;

// Initiate the calls and check for completion

do {

$mrc = curl_multi_exec($mh, $active);

} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {

if (curl_multi_select($mh) != -1) {

do {

$mrc = curl_multi_exec($mh, $active);

} while ($mrc == CURLM_CALL_MULTI_PERFORM);

}

}
<?php

// Create both cURL resources and add to cURL Multi

$ch1 = curl_init('http://www.timeapi.org/utc/now');

$ch2 = curl_init('http://www.timeapi.org/utc/now');

$mh = curl_multi_init();

curl_multi_add_handle($mh,$ch1);

curl_multi_add_handle($mh,$ch2);
E V E N T L O O P PA R A L L E L I S M
P R O C E S S
Q U E U E
I T E M
Q U E U E
I T E M
I N
Q U E U E ?
X
Yes
No
A S Y N C
P R O C E S S
A S Y N C
P R O C E S S
A S Y N C
P R O C E S S
A S Y N C
P R O C E S S
E V E N T L O O P E X A M P L E
<?php

# Build objects to handle asynchronous interaction

$loop = ReactEventLoopFactory::create();

$dnsFactory = new ReactDnsResolverFactory();

$dns = $dnsFactory->createCached('8.8.8.8', $loop);

$factory = new ReactHttpClientFactory();

$client = $factory->create($loop, $dns);

$resp = array();
# Create callback for handling response

$responseHandler = function ($response) use ($resp) {

$response->on( 'data', function ($data) use ($resp) {

$resp[] = $data;

});

};
# Queue up requests to send

$request = $client->request('GET', 'http://www.timeapi.org/utc/now');

$request->on('response', $responseHandler);

$request->end();



$request = $client->request('GET', 'http://www.timeapi.org/utc/now');

$request->on('response', $responseHandler);

$request->end();



# Run the loop

$loop->run();
F O R K / J O I N V S E V E N T L O O P
• Enhancing existing
traditional
synchronous apps
• Hack/HHVM
• Non-HTTP apps
• Full async apps
• High volume apps
F O R K J O I N E V E N T L O O P
C A L L B A C K S V S . G E N E R AT O R S
C A L L B A C K S V S . G E N E R AT O R S
class MyRequestHandler implements RequestHandler {



public function onRequest(Request $request, Socket $socket)

{

$response = new BasicResponse(Response::OK, [

'Content-Type' => 'text/plain',

]);

yield from $response->getBody()->end('Hello, world!');

return $response;

}



public function onError(int $code, Socket $socket)

{

return new BasicResponse($code);

}

}



$server = new Server(new MyRequestHandler());

$server->listen(8080);

Looprun();

$loop = ReactEventLoopFactory::create();

$socket = new ReactSocketServer($loop);



$http = new ReactHttpServer($socket);

$http->on('request', function ($request, $response) {

$response->writeHead(200, [‘Content-Type' => ‘text/plain']);

$response->end("Hello World!n");

});



$socket->listen(8080);

$loop->run();

Callback Generators
W H AT L I E S A H E A D ?
R E S O U R C E S
amphp icicle.io ReactPHP
PHP Asynchronous Interoperability Group
RecoilHack/HHVM
Q U E S T I O N S ?
https://joind.in/19455
P L E A S E R A T E T H I S TA L K !
P H O T O C R E D I T S B Y S L I D E
1: By SteveD. (Flickr) [CC BY 2.0 (http://creativecommons.org/licenses/by/2.0)], via Wikimedia Commons
3: Copyright 2016 by Adam Englander. All rights reserved.
5: By Strober (Own work) [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons
8: By Retro-Computing Society of Rhode Island - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/
index.php?curid=5094687
11: By Victorgrigas - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=20348454
15/16: By Justin De La Ornellas from China Town, Hawaii (avex18 Uploaded by clusternote) [CC BY 2.0 (http://
creativecommons.org/licenses/by/2.0)], via Wikimedia Commons
19: By Yoga Balaji - Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=35101769
21 Left: User: (WT-shared) Jpatokal at wts wikivoyage [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/
3.0)], via Wikimedia Commons
All others are public domain

More Related Content

What's hot (20)

PDF
2010 Smith Scripting101
bokonen
 
PDF
PHP7 - Scalar Type Hints & Return Types
Eric Poe
 
PPTX
Php 7 hhvm and co
Pierre Joye
 
PDF
Key features PHP 5.3 - 5.6
Federico Damián Lozada Mosto
 
PDF
How to inspect a RUNNING perl process
Masaaki HIROSE
 
PDF
How to deploy node to production
Sean Hess
 
ODP
The why and how of moving to PHP 5.5/5.6
Wim Godden
 
ODP
The why and how of moving to php 5.4
Wim Godden
 
ODP
The why and how of moving to PHP 5.4/5.5
Wim Godden
 
PDF
An introduction to PHP 5.4
Giovanni Derks
 
KEY
Anatomy of a PHP Request ( UTOSC 2010 )
Joseph Scott
 
ODP
Caching and tuning fun for high scalability @ FrOSCon 2011
Wim Godden
 
PPTX
Introducing PHP Latest Updates
Iftekhar Eather
 
ODP
Nigel hamilton-megameet-2013
trexy
 
ODP
Php in 2013 (Web-5 2013 conference)
julien pauli
 
PDF
What's new in PHP 5.5
Tom Corrigan
 
ODP
PHP5.5 is Here
julien pauli
 
PDF
Créer une base NoSQL en 1 heure
Amaury Bouchard
 
PPTX
Php7 hhvm and co
Pierre Joye
 
KEY
plackdo, plack-like web interface on perl6
Nobuo Danjou
 
2010 Smith Scripting101
bokonen
 
PHP7 - Scalar Type Hints & Return Types
Eric Poe
 
Php 7 hhvm and co
Pierre Joye
 
Key features PHP 5.3 - 5.6
Federico Damián Lozada Mosto
 
How to inspect a RUNNING perl process
Masaaki HIROSE
 
How to deploy node to production
Sean Hess
 
The why and how of moving to PHP 5.5/5.6
Wim Godden
 
The why and how of moving to php 5.4
Wim Godden
 
The why and how of moving to PHP 5.4/5.5
Wim Godden
 
An introduction to PHP 5.4
Giovanni Derks
 
Anatomy of a PHP Request ( UTOSC 2010 )
Joseph Scott
 
Caching and tuning fun for high scalability @ FrOSCon 2011
Wim Godden
 
Introducing PHP Latest Updates
Iftekhar Eather
 
Nigel hamilton-megameet-2013
trexy
 
Php in 2013 (Web-5 2013 conference)
julien pauli
 
What's new in PHP 5.5
Tom Corrigan
 
PHP5.5 is Here
julien pauli
 
Créer une base NoSQL en 1 heure
Amaury Bouchard
 
Php7 hhvm and co
Pierre Joye
 
plackdo, plack-like web interface on perl6
Nobuo Danjou
 

Viewers also liked (13)

PDF
php[world] 2016 - Tales From the Crypto: A Cryptography Primer
Adam Englander
 
PPTX
IoT Lockdown
Adam Englander
 
PDF
Node.js interactive NA 2016: Tales From the Crypt
Adam Englander
 
PPTX
Python Bluetooth
Adam Englander
 
PDF
Py Vegas - Tales from the crypt
Adam Englander
 
PPTX
Travis CI - PHP
Adam Englander
 
PDF
Asynchronous PHP and Real-time Messaging
Steve Rhoades
 
PDF
Phone calls and sms from php
David Stockton
 
PDF
IoT Lock Down - Battling the Bot Net Builders
Adam Englander
 
PDF
SunshinePHP 2017: Tales From The Crypt - A Cryptography Primer
Adam Englander
 
PDF
PHP UK 2017 - Don't Lose Sleep - Secure Your REST
Adam Englander
 
ODP
The promise of asynchronous PHP
Wim Godden
 
PDF
Biometrics - Fantastic Failure Point of the Future
Adam Englander
 
php[world] 2016 - Tales From the Crypto: A Cryptography Primer
Adam Englander
 
IoT Lockdown
Adam Englander
 
Node.js interactive NA 2016: Tales From the Crypt
Adam Englander
 
Python Bluetooth
Adam Englander
 
Py Vegas - Tales from the crypt
Adam Englander
 
Travis CI - PHP
Adam Englander
 
Asynchronous PHP and Real-time Messaging
Steve Rhoades
 
Phone calls and sms from php
David Stockton
 
IoT Lock Down - Battling the Bot Net Builders
Adam Englander
 
SunshinePHP 2017: Tales From The Crypt - A Cryptography Primer
Adam Englander
 
PHP UK 2017 - Don't Lose Sleep - Secure Your REST
Adam Englander
 
The promise of asynchronous PHP
Wim Godden
 
Biometrics - Fantastic Failure Point of the Future
Adam Englander
 
Ad

Similar to Zend con 2016 - Asynchronous Prorgamming in PHP (20)

PPTX
Apache Spark: the next big thing? - StampedeCon 2014
StampedeCon
 
PDF
Meteor WWNRW Intro
Stephan Hochhaus
 
PDF
Meteor - not just for rockstars
Stephan Hochhaus
 
PDF
PyLadies Talk: Learn to love the command line!
Blanca Mancilla
 
PDF
Strangler Pattern in practice @PHPers Day 2019
Michał Kurzeja
 
PDF
Java 20
Joe Kutner
 
PDF
Devinsampa nginx-scripting
Tony Fabeen
 
PDF
Nginx Scripting - Extending Nginx Functionalities with Lua
Tony Fabeen
 
PDF
Testing TYPO3 Applications
André Wuttig
 
PDF
Marko Gargenta_Remixing android
Droidcon Berlin
 
PDF
Model Serving via Pulsar Functions
Arun Kejariwal
 
PDF
Monitoring and Logging in Wonderland
Paul Seiffert
 
PDF
Reducing Resistance: Deployment as Surface
Jeffrey Hulten
 
PDF
Microservices With Spring Boot and Spring Cloud Netflix
Krzysztof Sobkowiak
 
PDF
Understanding and Implementing Website Security
Drew Gorton
 
PDF
Witchcraft
Brooklyn Zelenka
 
PDF
Bringing choas to order in your node.js app
Dan Jenkins
 
PDF
Profiling Web Archives IIPC GA 2015
Sawood Alam
 
PDF
Fast api
Simone Di Maulo
 
PDF
ZendCon 2017 - Build a Bot Workshop - Async Primer
Adam Englander
 
Apache Spark: the next big thing? - StampedeCon 2014
StampedeCon
 
Meteor WWNRW Intro
Stephan Hochhaus
 
Meteor - not just for rockstars
Stephan Hochhaus
 
PyLadies Talk: Learn to love the command line!
Blanca Mancilla
 
Strangler Pattern in practice @PHPers Day 2019
Michał Kurzeja
 
Java 20
Joe Kutner
 
Devinsampa nginx-scripting
Tony Fabeen
 
Nginx Scripting - Extending Nginx Functionalities with Lua
Tony Fabeen
 
Testing TYPO3 Applications
André Wuttig
 
Marko Gargenta_Remixing android
Droidcon Berlin
 
Model Serving via Pulsar Functions
Arun Kejariwal
 
Monitoring and Logging in Wonderland
Paul Seiffert
 
Reducing Resistance: Deployment as Surface
Jeffrey Hulten
 
Microservices With Spring Boot and Spring Cloud Netflix
Krzysztof Sobkowiak
 
Understanding and Implementing Website Security
Drew Gorton
 
Witchcraft
Brooklyn Zelenka
 
Bringing choas to order in your node.js app
Dan Jenkins
 
Profiling Web Archives IIPC GA 2015
Sawood Alam
 
Fast api
Simone Di Maulo
 
ZendCon 2017 - Build a Bot Workshop - Async Primer
Adam Englander
 
Ad

More from Adam Englander (20)

PPTX
Making PHP Smarter - Dutch PHP 2023.pptx
Adam Englander
 
PDF
Practical API Security - PyCon 2019
Adam Englander
 
PDF
Threat Modeling for Dummies
Adam Englander
 
PDF
ZendCon 2018 - Practical API Security
Adam Englander
 
PDF
ZendCon 2018 - Cryptography in Depth
Adam Englander
 
PDF
Threat Modeling for Dummies - Cascadia PHP 2018
Adam Englander
 
PDF
Dutch PHP 2018 - Cryptography for Beginners
Adam Englander
 
PDF
php[tek] 2108 - Cryptography Advances in PHP 7.2
Adam Englander
 
PDF
php[tek] 2018 - Biometrics, fantastic failure point of the future
Adam Englander
 
PDF
Biometrics: Sexy, Secure and... Stupid - RSAC 2018
Adam Englander
 
PPTX
Practical API Security - PyCon 2018
Adam Englander
 
PDF
Practical API Security - Midwest PHP 2018
Adam Englander
 
PDF
Cryptography for Beginners - Midwest PHP 2018
Adam Englander
 
PDF
Cryptography for Beginners - Sunshine PHP 2018
Adam Englander
 
PDF
ConFoo Vancouver 2017 - Biometrics: Fantastic Failure Point of the Future
Adam Englander
 
PDF
Con Foo 2017 - Don't Loose Sleep - Secure Your REST
Adam Englander
 
PDF
ZendCon 2017 - Cryptography for Beginners
Adam Englander
 
PDF
ZendCon 2017: The Red Team is Coming
Adam Englander
 
PDF
Symfony Live San Franciso 2017 - BDD API Development with Symfony and Behat
Adam Englander
 
PDF
Coder Cruise 2017 - The Red Team Is Coming
Adam Englander
 
Making PHP Smarter - Dutch PHP 2023.pptx
Adam Englander
 
Practical API Security - PyCon 2019
Adam Englander
 
Threat Modeling for Dummies
Adam Englander
 
ZendCon 2018 - Practical API Security
Adam Englander
 
ZendCon 2018 - Cryptography in Depth
Adam Englander
 
Threat Modeling for Dummies - Cascadia PHP 2018
Adam Englander
 
Dutch PHP 2018 - Cryptography for Beginners
Adam Englander
 
php[tek] 2108 - Cryptography Advances in PHP 7.2
Adam Englander
 
php[tek] 2018 - Biometrics, fantastic failure point of the future
Adam Englander
 
Biometrics: Sexy, Secure and... Stupid - RSAC 2018
Adam Englander
 
Practical API Security - PyCon 2018
Adam Englander
 
Practical API Security - Midwest PHP 2018
Adam Englander
 
Cryptography for Beginners - Midwest PHP 2018
Adam Englander
 
Cryptography for Beginners - Sunshine PHP 2018
Adam Englander
 
ConFoo Vancouver 2017 - Biometrics: Fantastic Failure Point of the Future
Adam Englander
 
Con Foo 2017 - Don't Loose Sleep - Secure Your REST
Adam Englander
 
ZendCon 2017 - Cryptography for Beginners
Adam Englander
 
ZendCon 2017: The Red Team is Coming
Adam Englander
 
Symfony Live San Franciso 2017 - BDD API Development with Symfony and Behat
Adam Englander
 
Coder Cruise 2017 - The Red Team Is Coming
Adam Englander
 

Recently uploaded (20)

PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 

Zend con 2016 - Asynchronous Prorgamming in PHP

  • 1. A S Y N C H R O N O U S P R O G R A M M I N G I N P H P Z E N D C O N 2 0 1 6
  • 2. W E L C O M E T O L A S V E G A S !
  • 3. W H O A M I ?
  • 4. W H Y A S Y N C H R O N O U S ?
  • 5. H I G H T R A F F I C
  • 6. R E A C T I V E A P P L I C AT I O N S
  • 7. I N T E R N E T O F T H I N G S
  • 8. S TAT E F U L P R O T O C O L S E R V E R S • WebSocket • MQTT • AMQP • IRC • XMPP • Telnet • SSH • RADIUS • LDAP • FTP
  • 9. H T T P / 2
  • 10. H O W D O Y O U D O A S Y N C ?
  • 11. S Y S T E M L E V E L A S Y N C • German • Apache Spark • AWS Lambda • Stomp • ZeroMQ • AMQP J O B S E R V E R S Q U E U I N G
  • 12. C O D E L E V E L A S Y N C • Forking and Threading • Asynchronous I/O • Fork/Join • Event Loop S T R AT E G I E S M E T H O D O L O G I E S
  • 13. F O R K I N G A N D T H R E A D I N G • Available in PHP core since PHP 4.1 • Requires the use of shared memory • Requires code to be written for management of forks/threads • Creates separate copies of code for each thread/ fork
  • 14. F O R K I N G / T H R E A D I N G L I B R A R I E S • PCNTL/POSIX — Forking and process management • pthreads — Thread management • Semaphore — Semaphore (locks) and shared memory • Shared Memory — Shared memory • Sync — Cross platform semaphore and shared memory
  • 15. A S Y N C H R O N O U S I / O • Frees up the current process while I/O is performed • Executes code based on I/O events • No copying of code for separate process
  • 16. R E A L W O R L D P R O F I L E D ATA C AT E G O RY S E G M E N T % T I M E AV G C A L L S ( P E R T X N ) AV G T I M E ( M S ) D ATA B A S E M Y S Q L A P P S S E L E C T 7 2 . 6 2 . 0 3 4 . 8 F U N C T I O N A P I . H A N D L E R S . A P P S : A P P H A N D L E R . R E N D E R _ R E S O U R C E 1 2 . 6 1 . 0 6 . 0 4 D ATA B A S E M Y S Q L A P P U S E R M A P S E L E C T 8 . 7 3 . 9 7 4 . 1 6 F U N C T I O N P Y R A M I D . R O U T E R : R O U T E R . _ _ C A L L _ _ 3 . 3 1 . 0 1 . 5 9 M Y S Q L O R G A N I Z AT I O N S S E L E C T 2 . 2 0 . 7 1 . 5 2
  • 17. A S Y N C H R O N O U S I / O L I B R A R I E S • Streams via stream_select and socket_select • eio — libeio • ev — libev • libevent — libevent • event — libevent
  • 18. F O R K / T H R E A D V S A S Y N C I / O • Compute heavy • Process isolation • Non-Async library • I/O processes • Process/memory optimization F O R K / T H R E A D A S Y N C / I O
  • 19. F O R K / J O I N PA R A L L E L I S M S TA RT P R O C E S S 1 P R O C E S S 2 P R O C E S S 3 E N DF O R K J O I N
  • 20. F O R K / J O I N E X A M P L E // JOIN
 curl_multi_remove_handle($mh, $ch1);
 curl_multi_remove_handle($mh, $ch2);
 curl_multi_close($mh);
 $resp = array(curl_multi_getcontent($ch1), curl_multi_getcontent($ch2)); // FORK
 $active = null;
 // Initiate the calls and check for completion
 do {
 $mrc = curl_multi_exec($mh, $active);
 } while ($mrc == CURLM_CALL_MULTI_PERFORM);
 while ($active && $mrc == CURLM_OK) {
 if (curl_multi_select($mh) != -1) {
 do {
 $mrc = curl_multi_exec($mh, $active);
 } while ($mrc == CURLM_CALL_MULTI_PERFORM);
 }
 } <?php
 // Create both cURL resources and add to cURL Multi
 $ch1 = curl_init('http://www.timeapi.org/utc/now');
 $ch2 = curl_init('http://www.timeapi.org/utc/now');
 $mh = curl_multi_init();
 curl_multi_add_handle($mh,$ch1);
 curl_multi_add_handle($mh,$ch2);
  • 21. E V E N T L O O P PA R A L L E L I S M P R O C E S S Q U E U E I T E M Q U E U E I T E M I N Q U E U E ? X Yes No A S Y N C P R O C E S S A S Y N C P R O C E S S A S Y N C P R O C E S S A S Y N C P R O C E S S
  • 22. E V E N T L O O P E X A M P L E <?php
 # Build objects to handle asynchronous interaction
 $loop = ReactEventLoopFactory::create();
 $dnsFactory = new ReactDnsResolverFactory();
 $dns = $dnsFactory->createCached('8.8.8.8', $loop);
 $factory = new ReactHttpClientFactory();
 $client = $factory->create($loop, $dns);
 $resp = array(); # Create callback for handling response
 $responseHandler = function ($response) use ($resp) {
 $response->on( 'data', function ($data) use ($resp) {
 $resp[] = $data;
 });
 }; # Queue up requests to send
 $request = $client->request('GET', 'http://www.timeapi.org/utc/now');
 $request->on('response', $responseHandler);
 $request->end();
 
 $request = $client->request('GET', 'http://www.timeapi.org/utc/now');
 $request->on('response', $responseHandler);
 $request->end();
 
 # Run the loop
 $loop->run();
  • 23. F O R K / J O I N V S E V E N T L O O P • Enhancing existing traditional synchronous apps • Hack/HHVM • Non-HTTP apps • Full async apps • High volume apps F O R K J O I N E V E N T L O O P
  • 24. C A L L B A C K S V S . G E N E R AT O R S
  • 25. C A L L B A C K S V S . G E N E R AT O R S class MyRequestHandler implements RequestHandler {
 
 public function onRequest(Request $request, Socket $socket)
 {
 $response = new BasicResponse(Response::OK, [
 'Content-Type' => 'text/plain',
 ]);
 yield from $response->getBody()->end('Hello, world!');
 return $response;
 }
 
 public function onError(int $code, Socket $socket)
 {
 return new BasicResponse($code);
 }
 }
 
 $server = new Server(new MyRequestHandler());
 $server->listen(8080);
 Looprun();
 $loop = ReactEventLoopFactory::create();
 $socket = new ReactSocketServer($loop);
 
 $http = new ReactHttpServer($socket);
 $http->on('request', function ($request, $response) {
 $response->writeHead(200, [‘Content-Type' => ‘text/plain']);
 $response->end("Hello World!n");
 });
 
 $socket->listen(8080);
 $loop->run();
 Callback Generators
  • 26. W H AT L I E S A H E A D ?
  • 27. R E S O U R C E S amphp icicle.io ReactPHP PHP Asynchronous Interoperability Group RecoilHack/HHVM
  • 28. Q U E S T I O N S ?
  • 30. P H O T O C R E D I T S B Y S L I D E 1: By SteveD. (Flickr) [CC BY 2.0 (http://creativecommons.org/licenses/by/2.0)], via Wikimedia Commons 3: Copyright 2016 by Adam Englander. All rights reserved. 5: By Strober (Own work) [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons 8: By Retro-Computing Society of Rhode Island - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/ index.php?curid=5094687 11: By Victorgrigas - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=20348454 15/16: By Justin De La Ornellas from China Town, Hawaii (avex18 Uploaded by clusternote) [CC BY 2.0 (http:// creativecommons.org/licenses/by/2.0)], via Wikimedia Commons 19: By Yoga Balaji - Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=35101769 21 Left: User: (WT-shared) Jpatokal at wts wikivoyage [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/ 3.0)], via Wikimedia Commons All others are public domain