Slide 1

Slide 1 text

Asynchronous Awesome Eric Mann

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Problems this Won’t Solve Report generation PubSub events Parallel I/O Parallel data access

Slide 5

Slide 5 text

Naive Approaches to Async Pseudo-cron (WordPress native and tools like wp-async-task) Mechanical Turk-like job systems Using another language entirely

Slide 6

Slide 6 text

There’s a Better Way

Slide 7

Slide 7 text

Parallel vs Concurrent Concurrent Parallel (not covered today) Concurrent Parallel

Slide 8

Slide 8 text

Concurrent and Parallel Programming - Joe Armstrong https://joearms.github.io/published/2013-04-05-concurrent-and-parallel-programming.html

Slide 9

Slide 9 text

Tools Available Gearman RabbitMQ Amp (promises) pthreads

Slide 10

Slide 10 text

Gearman

Slide 11

Slide 11 text

Initialize a Standalone Worker addServer('localhost', 4730); $worker->addFunction('import', array($importer, 'import'); while($worker->work());

Slide 12

Slide 12 text

Dispatch Jobs to the Worker addServer('localhost', 4730); $job = $client->doBackground('import', $file . '|:|' . $email); $db->record($job);

Slide 13

Slide 13 text

RabbitMQ

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Publish to the Queue channel(); $channel->queue_declare('demo', false, false, false, false); $msg = new AMQPMessage('Hello World!'); $channel->basic_publish($msg, '', 'demo'); $channel->close(); $connection->close();

Slide 16

Slide 16 text

Consume from the Queue channel(); $channel->queue_declare('demo', false, false, false, false); $cb = function($msg) { // Send Tweet with $msg contents } $channel->basic_consume('demo', '', false, true, false, false, $cb); while(count($channel->callbacks)) { $channel->wait(); }

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Interact with Multiple Queues queue_declare('demo', false, false, false, false); $channel->queue_declare('demo-rc', false, false, false, false); $cb = function($msg) { // Send Tweet with $msg contents $result = new AMQPMessage( /* Tweet ID */ ); $channel->basic_publish($result, '', 'demo-rc'); } $channel->basic_consume('demo', '', false, true, false, false, $cb); while(count($channel->callbacks)) { $channel->wait(); }

Slide 19

Slide 19 text

Amp (promises)

Slide 20

Slide 20 text

Deferred Empowers Async resolve($x * $y); }); return $deferred->promise(); } asyncMultiply(6, 7)->onResolve(function ($err, $result) { var_dump($result); // int(42) });

Slide 21

Slide 21 text

Chain Multiple Promises Together "http://www.google.com", "bing" => "http://www.bing.com", ]; $responses = yield array_map(function ($uri) use ($httpClient) { return $httpClient->request($uri); }, $uris); foreach ($responses as $key => $response) { printf("%s | %d\n", $key, $response->getStatus()); } Loop::stop(); });

Slide 22

Slide 22 text

pthreads

Slide 23

Slide 23 text

Create a Worker workerId = $id; } public function run() { // Do some heavy processing } }

Slide 24

Slide 24 text

Start the Workers start(); // Now they'll each run independently } foreach (range(0, 5) as $id) { $workers[$id]->join(); // Synchronously rejoin the main process }

Slide 25

Slide 25 text

What Use is Any of This? Gearman – powers things like PDF conversion, video transpiling, APNS RabbitMQ – powers cross-app messaging in load balanced environments Promises – power concurrent data migrations for large data sources pthreads – powers task management in a performance, scalable, managed fashion

Slide 26

Slide 26 text

Further Study The amphp/parallel library wraps pthreads entirely – use both at the same time! Look at what the Node community is doing – we have the same exact tools available, but a much larger community and lower barriers Challenge yourself to look at traditional programming problems from an asynchronous viewpoint – can things run faster in parallel?

Slide 27

Slide 27 text

Questions?

Slide 28

Slide 28 text

Thank you [email protected] | 503.925.6266