php[world] 2016 - you don’t need node.js - async programming in php

31
YOU DON’T NEED NODE.JS: ASYNC PROGRAMMING IN PHP PHP[WORLD] 2016

Upload: adam-englander

Post on 23-Jan-2018

240 views

Category:

Software


0 download

TRANSCRIPT

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

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

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

W H O A M I ?

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

W H Y A S Y N C H R O N O U S ?

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

H I G H T R A F F I C

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

R E A C T I V E A P P L I C AT I O N S

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

I N T E R N E T O F T H I N G S

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

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

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

H T T P / 2

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

H O W D O Y O U D O A S Y N C ?

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

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

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

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

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

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

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

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

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

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

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

R E A L W O R L D P R O F I L E D ATAC AT E G O R Y 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 NA P I . H A N D L E R S . A P P S : A P P H A ND 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 NP 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

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

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

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

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

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

F O R K / J O I N PA R A L L E L I S M

S TA R T

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

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

F O R K / J O I N E X A M P L E : C U R L

// JOINcurl_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 completiondo { $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);

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

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 \GuzzleHttp\Client(); $times = array();

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

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

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

E V E N T L O O P E X A M P L E<?php# Build objects to handle asynchronous interaction$loop = React\EventLoop\Factory::create(); $dnsFactory = new React\Dns\Resolver\Factory(); $dns = $dnsFactory->createCached('8.8.8.8', $loop); $factory = new React\HttpClient\Factory(); $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();

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

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

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

C A L L B A C K S V S . G E N E R AT O R S

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

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); Loop\run();

$loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $http = new React\Http\Server($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

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

W H AT L I E S A H E A D ?

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

R E S O U R C E S

amphp icicle.io ReactPHP

PHP Asynchronous Interoperability Group

RecoilHack/HHVM

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

Q U E S T I O N S ?

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

https://joind.in/talk/bec6cP L E A S E R A T E T H I S TA L K !

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

20162016

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

P H O T O C R E D I T S B Y S L I D E1: 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