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

50 %
50 %
Information about php[world] 2016 - You Don’t Need Node.js - Async Programming in PHP
j s

Published on November 18, 2016

Author: AdamEnglander

Source: slideshare.net

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 ?

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

30. 20162016

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

Add a comment