Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
@adam_englander
Build A Bot Workshop:
Async Primer
Adam Englander
iovation
@adam_englander
What You Will Need
• PHP 7.0, or 7.1 (Examples will be 7.1)
• intl extension
• Twitter account
• Twitter D...
@adam_englander
What We Will Discuss
• Concurrent Programming
• Asynchronous I/O
• AMPHP Asynchronous Framework for PHP
@adam_englander
What We Will Do
1. We will use an asynchronous HTTP client to
execute a Twitter REST API command.
2. We wi...
@adam_englander
Concurrent Programming
• Forking and Threading
• Asynchronous I/O
• Fork/Join
• Event Loop
Strategies Meth...
@adam_englander
Forking and Threading
• Available in PHP core since PHP 4.1
• Requires the use of shared memory
• Requires...
@adam_englander
Asynchronous I/O
• Frees up the current process while I/O is
performed
• Executes code based on I/O events...
@adam_englander
Asynchronous I/O Libraries
• Streams via stream_select and socket_select
• eio — libeio
• ev — libev
• lib...
@adam_englander
Fork/Thread vs Async I/O
• Compute heavy
• Process isolation
• Non-Async library
• I/O processes
• Process...
@adam_englander
Fork/Join Parallelism
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
Fork/Join Example
$dispatcher = new AmpThreadDispatcher();

$a = $dispatcher->call('slowProcess1');

$b = $dispatcher->cal...
@adam_englander
Event Loop Parallelism
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 ...
Event Loop Example
<?php

# Build objects to handle asynchronous interaction

$loop = ReactEventLoopFactory::create();

$d...
@adam_englander
Fork/Join vs Event Loop
• Enhance existing
synchronous apps
• Promises
• Hack/HHVM
• Non-HTTP apps
• Full ...
Callbacks vs. Generators
$stream = yield $apiClient->request($request);

while (null !== $message = yield $stream->read())...
@adam_englander
AMPHP
Asynchronous Framework for PHP
@adam_englander
Complete Modern Framework
• Modular framework segmented into logical parts
• Supersedes icicle.io
• Driver...
@adam_englander
Code Time!
@adam_englander
• http://amphp.org/
• #amphp on Freenode IRC
• http://blog.kelunik.com/
• https://dev.twitter.com/
• https...
@adam_englander
Please Rate Me
https://joind.in/talk/e9da4
Upcoming SlideShare
Loading in …5
×

ZendCon 2017 - Build a Bot Workshop - Async Primer

Asynchronous software development is rapidly moving from the niche to the mainstream. That mainstream now includes PHP. This workshop will give you hands on instruction in building an asynchronous application in PHP. We'll build a Twitter Bot utilizing the Amp concurrency framework for PHP and the Twitter Streaming API. During this time you'll learn the basics regarding the Amp event loop, generators and co-routines, and writing non-blocking code. Get ready for the future of PHP today.

Related Books

Free with a 30 day trial from Scribd

See all
  • Login to see the comments

  • Be the first to like this

ZendCon 2017 - Build a Bot Workshop - Async Primer

  1. 1. @adam_englander Build A Bot Workshop: Async Primer Adam Englander iovation
  2. 2. @adam_englander What You Will Need • PHP 7.0, or 7.1 (Examples will be 7.1) • intl extension • Twitter account • Twitter Developer App: https://apps.twitter.com • OAuth Credentials: https://dev.twitter.com/oauth/ overview/application-owner-access-tokens
  3. 3. @adam_englander What We Will Discuss • Concurrent Programming • Asynchronous I/O • AMPHP Asynchronous Framework for PHP
  4. 4. @adam_englander What We Will Do 1. We will use an asynchronous HTTP client to execute a Twitter REST API command. 2. We will use an asynchronous HTTP client to process a continuous stream via the Twitter User Stream API. 3. We will leverage both of the above to listen to a topic and automatically respond to updates.
  5. 5. @adam_englander Concurrent Programming • Forking and Threading • Asynchronous I/O • Fork/Join • Event Loop Strategies Methodologies
  6. 6. @adam_englander Forking and Threading • 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
  7. 7. @adam_englander Asynchronous 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
  8. 8. @adam_englander Asynchronous I/O Libraries • Streams via stream_select and socket_select • eio — libeio • ev — libev • libevent — libevent • event — libevent
  9. 9. @adam_englander Fork/Thread vs Async I/O • Compute heavy • Process isolation • Non-Async library • I/O processes • Process/memory optimization Fork/Thread Async I/O
  10. 10. @adam_englander Fork/Join Parallelism 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
  11. 11. Fork/Join Example $dispatcher = new AmpThreadDispatcher();
 $a = $dispatcher->call('slowProcess1');
 $b = $dispatcher->call('slowProcess2');
 $c = $dispatcher->call('slowProcess3');
 
 $comboPromise = Ampall([$a, $b, $c]);
 list($a, $b, $c) = Ampwait($comboPromise);
  12. 12. @adam_englander Event Loop Parallelism 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
  13. 13. Event Loop Example <?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();
  14. 14. @adam_englander Fork/Join vs Event Loop • Enhance existing synchronous apps • Promises • Hack/HHVM • Non-HTTP apps • Full async apps • High volume apps Fork/Join Event Loop
  15. 15. Callbacks vs. Generators $stream = yield $apiClient->request($request);
 while (null !== $message = yield $stream->read()) {
 print($message[‘user’][‘screen_name’].": “.$message[‘text']);
 } Callback Generator Aerysrouter()
 ->get("/", function (AerysRequest $req, AerysResponse $resp) {
 $resp->end("<h1>Hello World!</h1>");
 })
 ->head("/", function (AerysRequest $req, AerysResponse $resp) {
 $resp->end("<h1>Hello Head!</h1>");
 });
  16. 16. @adam_englander AMPHP Asynchronous Framework for PHP
  17. 17. @adam_englander Complete Modern Framework • Modular framework segmented into logical parts • Supersedes icicle.io • Drivers for common storage engines • Can be used for event loop and fork/join • Can use threads or async i/o
  18. 18. @adam_englander Code Time!
  19. 19. @adam_englander • http://amphp.org/ • #amphp on Freenode IRC • http://blog.kelunik.com/ • https://dev.twitter.com/ • https://github.com/PeeHaa/AsyncTwitter • https://github.com/aenglander/amp-twitter-bot Resources
  20. 20. @adam_englander Please Rate Me https://joind.in/talk/e9da4

×