SlideShare a Scribd company logo
Y O U D O N ’ T N E E D N O D E . J S :
A S Y N C P R O G R A M M I N G I N P H P
P H P [ W O R L D ] 2 0 1 6
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
• Gearman
• 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 : C U R L
// 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);

}

}
// 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);
F O R K / J O I N E X A M P L E : G U Z Z L E
// JOIN

$promise1->wait();

$promise2->wait();
// FORK

$handler = function ($response) use (&$times) {

$times[] = $response

->getBody()

->read(512);

};



$promise1 = $client

->getAsync('http://www.timeapi.org/utc/now')

->then($handler);

$promise2 = $client

->getAsync('http://www.timeapi.org/utc/now')

->then($handler);
// Create client, request, and responses array

$client = new GuzzleHttpClient();

$times = array();
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
• Enhance existing
synchronous apps
• Promises
• 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/talk/bec6c
P L E A S E R A T E T H I S TA L K !
20162016
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
2: Copyright 2016 by Adam Englander. All rights reserved.
4: By Strober (Own work) [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons
7: By Retro-Computing Society of Rhode Island - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/
index.php?curid=5094687
10: By Victorgrigas - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=20348454
14/15: 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
18: By Yoga Balaji - Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=35101769
20 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
Ad

Recommended

Zend con 2016 - Asynchronous Prorgamming in PHP
Zend con 2016 - Asynchronous Prorgamming in PHP
Adam Englander
 
Codes
Codes
Narayan Loke
 
Anatomy of a PHP Request ( UTOSC 2010 )
Anatomy of a PHP Request ( UTOSC 2010 )
Joseph Scott
 
2010 Smith Scripting101
2010 Smith Scripting101
bokonen
 
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
various tricks for remote linux exploits  by Seok-Ha Lee (wh1ant)
CODE BLUE
 
Nigel hamilton-megameet-2013
Nigel hamilton-megameet-2013
trexy
 
Publishing a Perl6 Module
Publishing a Perl6 Module
ast_j
 
plackdo, plack-like web interface on perl6
plackdo, plack-like web interface on perl6
Nobuo Danjou
 
Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed Perl
Hideaki Ohno
 
2010/7/31 LTの虎@LL Tiger
2010/7/31 LTの虎@LL Tiger
Akihiro Okuno
 
Impala: A Modern, Open-Source SQL Engine for Hadoop
Impala: A Modern, Open-Source SQL Engine for Hadoop
All Things Open
 
Redis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your application
rjsmelo
 
ZeroMQ Is The Answer
ZeroMQ Is The Answer
Ian Barber
 
Meet up symfony 16 juin 2017 - Les PSR
Meet up symfony 16 juin 2017 - Les PSR
Julien Vinber
 
Créer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heure
Amaury Bouchard
 
Hachiojipm11
Hachiojipm11
Hideaki Ohno
 
Joy of Six - Discover the Joy of Perl 6
Joy of Six - Discover the Joy of Perl 6
trexy
 
Introducing PHP Latest Updates
Introducing PHP Latest Updates
Iftekhar Eather
 
Devinsampa nginx-scripting
Devinsampa nginx-scripting
Tony Fabeen
 
Отладка в GDB
Отладка в GDB
Anthony Shoumikhin
 
32 shell-programming
32 shell-programming
kayalkarnan
 
ZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made Simple
Ian Barber
 
On UnQLite
On UnQLite
charsbar
 
What's new in PHP 5.5
What's new in PHP 5.5
Tom Corrigan
 
Clone database using_active_duplicate
Clone database using_active_duplicate
Viaggio Italia
 
Using the Power to Prove
Using the Power to Prove
Kazuho Oku
 
Git::Hooks
Git::Hooks
Mikko Koivunalho
 
How to stand on the shoulders of giants
How to stand on the shoulders of giants
Ian Barber
 
Phone calls and sms from php
Phone calls and sms from php
David Stockton
 
IoT Lock Down - Battling the Bot Net Builders
IoT Lock Down - Battling the Bot Net Builders
Adam Englander
 

More Related Content

What's hot (20)

Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed Perl
Hideaki Ohno
 
2010/7/31 LTの虎@LL Tiger
2010/7/31 LTの虎@LL Tiger
Akihiro Okuno
 
Impala: A Modern, Open-Source SQL Engine for Hadoop
Impala: A Modern, Open-Source SQL Engine for Hadoop
All Things Open
 
Redis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your application
rjsmelo
 
ZeroMQ Is The Answer
ZeroMQ Is The Answer
Ian Barber
 
Meet up symfony 16 juin 2017 - Les PSR
Meet up symfony 16 juin 2017 - Les PSR
Julien Vinber
 
Créer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heure
Amaury Bouchard
 
Hachiojipm11
Hachiojipm11
Hideaki Ohno
 
Joy of Six - Discover the Joy of Perl 6
Joy of Six - Discover the Joy of Perl 6
trexy
 
Introducing PHP Latest Updates
Introducing PHP Latest Updates
Iftekhar Eather
 
Devinsampa nginx-scripting
Devinsampa nginx-scripting
Tony Fabeen
 
Отладка в GDB
Отладка в GDB
Anthony Shoumikhin
 
32 shell-programming
32 shell-programming
kayalkarnan
 
ZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made Simple
Ian Barber
 
On UnQLite
On UnQLite
charsbar
 
What's new in PHP 5.5
What's new in PHP 5.5
Tom Corrigan
 
Clone database using_active_duplicate
Clone database using_active_duplicate
Viaggio Italia
 
Using the Power to Prove
Using the Power to Prove
Kazuho Oku
 
Git::Hooks
Git::Hooks
Mikko Koivunalho
 
How to stand on the shoulders of giants
How to stand on the shoulders of giants
Ian Barber
 
Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed Perl
Hideaki Ohno
 
2010/7/31 LTの虎@LL Tiger
2010/7/31 LTの虎@LL Tiger
Akihiro Okuno
 
Impala: A Modern, Open-Source SQL Engine for Hadoop
Impala: A Modern, Open-Source SQL Engine for Hadoop
All Things Open
 
Redis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your application
rjsmelo
 
ZeroMQ Is The Answer
ZeroMQ Is The Answer
Ian Barber
 
Meet up symfony 16 juin 2017 - Les PSR
Meet up symfony 16 juin 2017 - Les PSR
Julien Vinber
 
Créer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heure
Amaury Bouchard
 
Joy of Six - Discover the Joy of Perl 6
Joy of Six - Discover the Joy of Perl 6
trexy
 
Introducing PHP Latest Updates
Introducing PHP Latest Updates
Iftekhar Eather
 
Devinsampa nginx-scripting
Devinsampa nginx-scripting
Tony Fabeen
 
32 shell-programming
32 shell-programming
kayalkarnan
 
ZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made Simple
Ian Barber
 
On UnQLite
On UnQLite
charsbar
 
What's new in PHP 5.5
What's new in PHP 5.5
Tom Corrigan
 
Clone database using_active_duplicate
Clone database using_active_duplicate
Viaggio Italia
 
Using the Power to Prove
Using the Power to Prove
Kazuho Oku
 
How to stand on the shoulders of giants
How to stand on the shoulders of giants
Ian Barber
 

Viewers also liked (20)

Phone calls and sms from php
Phone calls and sms from php
David Stockton
 
IoT Lock Down - Battling the Bot Net Builders
IoT Lock Down - Battling the Bot Net Builders
Adam Englander
 
SunshinePHP 2017: Tales From The Crypt - A Cryptography Primer
SunshinePHP 2017: Tales From The Crypt - A Cryptography Primer
Adam Englander
 
PHP UK 2017 - Don't Lose Sleep - Secure Your REST
PHP UK 2017 - Don't Lose Sleep - Secure Your REST
Adam Englander
 
The promise of asynchronous PHP
The promise of asynchronous PHP
Wim Godden
 
IoT Lockdown
IoT Lockdown
Adam Englander
 
Py Vegas - Tales from the crypt
Py Vegas - Tales from the crypt
Adam Englander
 
php[world] 2016 - Tales From the Crypto: A Cryptography Primer
php[world] 2016 - Tales From the Crypto: A Cryptography Primer
Adam Englander
 
Node.js interactive NA 2016: Tales From the Crypt
Node.js interactive NA 2016: Tales From the Crypt
Adam Englander
 
Python Bluetooth
Python Bluetooth
Adam Englander
 
Travis CI - PHP
Travis CI - PHP
Adam Englander
 
Asynchronous I/O in PHP
Asynchronous I/O in PHP
Thomas Weinert
 
Asynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time Messaging
Steve Rhoades
 
Adding 1.21 Gigawatts to Applications with RabbitMQ (Bulgaria PHP 2016 - Tuto...
Adding 1.21 Gigawatts to Applications with RabbitMQ (Bulgaria PHP 2016 - Tuto...
James Titcumb
 
Hack the Future
Hack the Future
Jason McCreary
 
Create, test, secure, repeat
Create, test, secure, repeat
Michelangelo van Dam
 
php[world] 2015 Training - Laravel from the Ground Up
php[world] 2015 Training - Laravel from the Ground Up
Joe Ferguson
 
Engineer - Mastering the Art of Software
Engineer - Mastering the Art of Software
Cristiano Diniz da Silva
 
Zend Framework Foundations
Zend Framework Foundations
Chuck Reeves
 
Amp your site an intro to accelerated mobile pages
Amp your site an intro to accelerated mobile pages
Robert McFrazier
 
Phone calls and sms from php
Phone calls and sms from php
David Stockton
 
IoT Lock Down - Battling the Bot Net Builders
IoT Lock Down - Battling the Bot Net Builders
Adam Englander
 
SunshinePHP 2017: Tales From The Crypt - A Cryptography Primer
SunshinePHP 2017: Tales From The Crypt - A Cryptography Primer
Adam Englander
 
PHP UK 2017 - Don't Lose Sleep - Secure Your REST
PHP UK 2017 - Don't Lose Sleep - Secure Your REST
Adam Englander
 
The promise of asynchronous PHP
The promise of asynchronous PHP
Wim Godden
 
Py Vegas - Tales from the crypt
Py Vegas - Tales from the crypt
Adam Englander
 
php[world] 2016 - Tales From the Crypto: A Cryptography Primer
php[world] 2016 - Tales From the Crypto: A Cryptography Primer
Adam Englander
 
Node.js interactive NA 2016: Tales From the Crypt
Node.js interactive NA 2016: Tales From the Crypt
Adam Englander
 
Asynchronous I/O in PHP
Asynchronous I/O in PHP
Thomas Weinert
 
Asynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time Messaging
Steve Rhoades
 
Adding 1.21 Gigawatts to Applications with RabbitMQ (Bulgaria PHP 2016 - Tuto...
Adding 1.21 Gigawatts to Applications with RabbitMQ (Bulgaria PHP 2016 - Tuto...
James Titcumb
 
php[world] 2015 Training - Laravel from the Ground Up
php[world] 2015 Training - Laravel from the Ground Up
Joe Ferguson
 
Zend Framework Foundations
Zend Framework Foundations
Chuck Reeves
 
Amp your site an intro to accelerated mobile pages
Amp your site an intro to accelerated mobile pages
Robert McFrazier
 
Ad

Similar to php[world] 2016 - You Don’t Need Node.js - Async Programming in PHP (20)

ZendCon 2017 - Build a Bot Workshop - Async Primer
ZendCon 2017 - Build a Bot Workshop - Async Primer
Adam Englander
 
Build a bot workshop async primer - php[tek]
Build a bot workshop async primer - php[tek]
Adam Englander
 
The promise of asynchronous php
The promise of asynchronous php
Wim Godden
 
Meet a parallel, asynchronous PHP world
Meet a parallel, asynchronous PHP world
Steve Maraspin
 
When symfony met promises
When symfony met promises
Marc Morera
 
The promise of asynchronous PHP
The promise of asynchronous PHP
Wim Godden
 
The promise of asynchronous php
The promise of asynchronous php
Wim Godden
 
Building Web APIs that Scale
Building Web APIs that Scale
Salesforce Developers
 
Asynchronous Architectures for Implementing Scalable Cloud Services - Evan Co...
Asynchronous Architectures for Implementing Scalable Cloud Services - Evan Co...
Twilio Inc
 
Concurrency, Parallelism And IO
Concurrency, Parallelism And IO
Piyush Katariya
 
React PHP: the NodeJS challenger
React PHP: the NodeJS challenger
vanphp
 
Netty: asynchronous data transfer
Netty: asynchronous data transfer
Victor Cherkassky
 
Session Server - Maintaing State between several Servers
Session Server - Maintaing State between several Servers
Stephan Schmidt
 
StrongLoop Overview
StrongLoop Overview
Shubhra Kar
 
Highly concurrent yet natural programming
Highly concurrent yet natural programming
Infinit
 
Asynchronous PHP - reactPHP et oui, ça existe!
Asynchronous PHP - reactPHP et oui, ça existe!
Vanessa V.Chellen
 
Introduction to Node.js
Introduction to Node.js
Richard Lee
 
PHP projects beyond the LAMP stack
PHP projects beyond the LAMP stack
Codemotion
 
node.js: Javascript's in your backend
node.js: Javascript's in your backend
David Padbury
 
From Generator to Fiber the Road to Coroutine in PHP
From Generator to Fiber the Road to Coroutine in PHP
Albert Chen
 
ZendCon 2017 - Build a Bot Workshop - Async Primer
ZendCon 2017 - Build a Bot Workshop - Async Primer
Adam Englander
 
Build a bot workshop async primer - php[tek]
Build a bot workshop async primer - php[tek]
Adam Englander
 
The promise of asynchronous php
The promise of asynchronous php
Wim Godden
 
Meet a parallel, asynchronous PHP world
Meet a parallel, asynchronous PHP world
Steve Maraspin
 
When symfony met promises
When symfony met promises
Marc Morera
 
The promise of asynchronous PHP
The promise of asynchronous PHP
Wim Godden
 
The promise of asynchronous php
The promise of asynchronous php
Wim Godden
 
Asynchronous Architectures for Implementing Scalable Cloud Services - Evan Co...
Asynchronous Architectures for Implementing Scalable Cloud Services - Evan Co...
Twilio Inc
 
Concurrency, Parallelism And IO
Concurrency, Parallelism And IO
Piyush Katariya
 
React PHP: the NodeJS challenger
React PHP: the NodeJS challenger
vanphp
 
Netty: asynchronous data transfer
Netty: asynchronous data transfer
Victor Cherkassky
 
Session Server - Maintaing State between several Servers
Session Server - Maintaing State between several Servers
Stephan Schmidt
 
StrongLoop Overview
StrongLoop Overview
Shubhra Kar
 
Highly concurrent yet natural programming
Highly concurrent yet natural programming
Infinit
 
Asynchronous PHP - reactPHP et oui, ça existe!
Asynchronous PHP - reactPHP et oui, ça existe!
Vanessa V.Chellen
 
Introduction to Node.js
Introduction to Node.js
Richard Lee
 
PHP projects beyond the LAMP stack
PHP projects beyond the LAMP stack
Codemotion
 
node.js: Javascript's in your backend
node.js: Javascript's in your backend
David Padbury
 
From Generator to Fiber the Road to Coroutine in PHP
From Generator to Fiber the Road to Coroutine in PHP
Albert Chen
 
Ad

More from Adam Englander (20)

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

Recently uploaded (20)

Best Software Development at Best Prices
Best Software Development at Best Prices
softechies7
 
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Muhammad Fahad Bashir
 
A Guide to Telemedicine Software Development.pdf
A Guide to Telemedicine Software Development.pdf
Olivero Bozzelli
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
WSO2
 
Building Geospatial Data Warehouse for GIS by GIS with FME
Building Geospatial Data Warehouse for GIS by GIS with FME
Safe Software
 
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
 
Complete WordPress Programming Guidance Book
Complete WordPress Programming Guidance Book
Shabista Imam
 
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
From Code to Commerce, a Backend Java Developer's Galactic Journey into Ecomm...
From Code to Commerce, a Backend Java Developer's Galactic Journey into Ecomm...
Jamie Coleman
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Zoho Creator Solution for EI by Elsner Technologies.docx
Zoho Creator Solution for EI by Elsner Technologies.docx
Elsner Technologies Pvt. Ltd.
 
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
Streamlining CI/CD with FME Flow: A Practical Guide
Streamlining CI/CD with FME Flow: A Practical Guide
Safe Software
 
Automated Testing and Safety Analysis of Deep Neural Networks
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
 
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
Introduction to Agile Frameworks for Product Managers.pdf
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed
 
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
 
Best Software Development at Best Prices
Best Software Development at Best Prices
softechies7
 
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Muhammad Fahad Bashir
 
A Guide to Telemedicine Software Development.pdf
A Guide to Telemedicine Software Development.pdf
Olivero Bozzelli
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
WSO2
 
Building Geospatial Data Warehouse for GIS by GIS with FME
Building Geospatial Data Warehouse for GIS by GIS with FME
Safe Software
 
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
IDM Crack with Internet Download Manager 6.42 Build 41 [Latest 2025]
pcprocore
 
Complete WordPress Programming Guidance Book
Complete WordPress Programming Guidance Book
Shabista Imam
 
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
From Code to Commerce, a Backend Java Developer's Galactic Journey into Ecomm...
From Code to Commerce, a Backend Java Developer's Galactic Journey into Ecomm...
Jamie Coleman
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Zoho Creator Solution for EI by Elsner Technologies.docx
Zoho Creator Solution for EI by Elsner Technologies.docx
Elsner Technologies Pvt. Ltd.
 
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
Streamlining CI/CD with FME Flow: A Practical Guide
Streamlining CI/CD with FME Flow: A Practical Guide
Safe Software
 
Automated Testing and Safety Analysis of Deep Neural Networks
Automated Testing and Safety Analysis of Deep Neural Networks
Lionel Briand
 
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
Introduction to Agile Frameworks for Product Managers.pdf
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed
 
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
 

php[world] 2016 - You Don’t Need Node.js - Async Programming in PHP

  • 1. Y O U D O N ’ T N E E D N O D E . J S : A S Y N C P R O G R A M M I N G I N P H P P H P [ W O R L D ] 2 0 1 6
  • 2. W H O A M I ?
  • 3. W H Y A S Y N C H R O N O U S ?
  • 4. H I G H T R A F F I C
  • 5. R E A C T I V E A P P L I C AT I O N S
  • 6. I N T E R N E T O F T H I N G S
  • 7. 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
  • 8. H T T P / 2
  • 9. H O W D O Y O U D O A S Y N C ?
  • 10. S Y S T E M L E V E L A S Y N C • Gearman • Apache Spark • AWS Lambda • Stomp • ZeroMQ • AMQP J O B S E R V E R S Q U E U I N G
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. F O R K / J O I N E X A M P L E : C U R L // 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);
 }
 } // 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);
  • 20. F O R K / J O I N E X A M P L E : G U Z Z L E // JOIN
 $promise1->wait();
 $promise2->wait(); // FORK
 $handler = function ($response) use (&$times) {
 $times[] = $response
 ->getBody()
 ->read(512);
 };
 
 $promise1 = $client
 ->getAsync('http://www.timeapi.org/utc/now')
 ->then($handler);
 $promise2 = $client
 ->getAsync('http://www.timeapi.org/utc/now')
 ->then($handler); // Create client, request, and responses array
 $client = new GuzzleHttpClient();
 $times = array();
  • 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 • Enhance existing synchronous apps • Promises • 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 ?
  • 31. 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 2: Copyright 2016 by Adam Englander. All rights reserved. 4: By Strober (Own work) [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons 7: By Retro-Computing Society of Rhode Island - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/ index.php?curid=5094687 10: By Victorgrigas - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=20348454 14/15: 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 18: By Yoga Balaji - Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=35101769 20 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