Slide 1

Slide 1 text

async php

Slide 2

Slide 2 text

thanks sunshine

Slide 3

Slide 3 text

thanks silverstripe

Slide 4

Slide 4 text

story 'me

Slide 5

Slide 5 text

you can write async code

Slide 6

Slide 6 text

spreading work to other threads

Slide 7

Slide 7 text

forking

Slide 8

Slide 8 text

$pid = pcntl_fork(); if ($pid) { // you're in the parent process pcntl_waitpid($pid, $status, WUNTRACED); if ($status > 0) { // handle the error } } else { // do something in the child process if ($failed) { exit(1); } exit(0); }

Slide 9

Slide 9 text

php.net/manual/en/book.pcntl.php

Slide 10

Slide 10 text

leanpub.com/signalingphp

Slide 11

Slide 11 text

threading

Slide 12

Slide 12 text

class MyThread extends Thread { public $hasLicense = null; private $repository; public function __construct($repository) { $this->repository = escapeshellarg($repository); } public function run() { $folder = hash("sha256", $this->repository); exec("git clone " . $this->repository . " " . $folder); chdir(__DIR__ . "/" . $folder); $this->hasLicense = preg_grep("/^LICENSE/i", glob("*")); } }

Slide 13

Slide 13 text

$thread = new MyThread("[email protected]:php/php-src.git"); if ($thread->start()) { $thread->join(); // do something with $thread->hasLicense }

Slide 14

Slide 14 text

php.net/manual/en/book.pthreads.php

Slide 15

Slide 15 text

pthreads.org

Slide 16

Slide 16 text

spreading the work to other machines

Slide 17

Slide 17 text

message queues

Slide 18

Slide 18 text

$pheanstalk = new Pheanstalk\Pheanstalk("127.0.0.1"); $pheanstalk->useTube("sunshine")->put("a message"); // some time later... $message = $pheanstalk->watch("sunshine")->reserve(); $pheanstalk->delete($message);

Slide 19

Slide 19 text

$context = new ZMQContext(); $socket = new ZMQSocket($context, ZMQ::SOCKET_PUSH); $socket->connect("tcp://127.0.0.1:5555"); $socket->send("a message"); // some time later $socket = new ZMQSocket($context, ZMQ::SOCKET_PULL); $socket->bind("tcp://127.0.0.1:5555"); $message = $socket->recv();

Slide 20

Slide 20 text

you can also use rabbit mq, amazon sqs, and iron mq

Slide 21

Slide 21 text

gearman

Slide 22

Slide 22 text

$client = new GearmanClient(); $client->addServer("127.0.0.1", 4730); $client->setCompleteCallback(function(GearmanTask $task) { // so domething when the task is complete }); $task = $client->addTask("inspect", "some data"); $client->runTasks();

Slide 23

Slide 23 text

$worker = new GearmanWorker(); $worker->addServer("127.0.0.1", 4730); $worker->addFunction("inspect", function(GearmanJob $job) { // do the job, with $job->workload() }); while ($worker->work()) { if ($worker->returnCode() !== GEARMAN_SUCCESS) { // something went wrong } }

Slide 24

Slide 24 text

php.net/manual/en/book.gearman.php

Slide 25

Slide 25 text

async standard library

Slide 26

Slide 26 text

amphp, icicle, react...

Slide 27

Slide 27 text

explicit event loops

Slide 28

Slide 28 text

use Icicle\Http\Message\BasicResponse; use Icicle\Http\Message\Request; use Icicle\Http\Server\RequestHandler; use Icicle\Socket\Socket; use Icicle\Stream\MemorySink; class MyRequestHandler implements RequestHandler { public function onRequest(Request $request, Socket $socket): Generator { $stream = new MemorySink(); yield from $stream->end("hello world"); yield new BasicResponse(200, [ "Content-Type" => "text/plain", "Content-Length" => $stream->getLength(), ], $stream); } public function onError(int $code, Socket $socket): Generator { yield new BasicResponse($code); } }

Slide 29

Slide 29 text

use Icicle\Http\Server\Server; use Icicle\Loop; $server = new Server(new MyRequestHandler()); $server->listen(81, "127.0.0.1"); $server->listen(82, "127.0.0.1"); Loop\run();

Slide 30

Slide 30 text

server { listen 80; server_name acme.com; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_pass http://127.0.0.1:81; } }

Slide 31

Slide 31 text

use Icicle\Http\Message\Request; use Icicle\Http\Message\Response; use Icicle\Socket\Socket; use Icicle\WebSocket\Application; use Icicle\WebSocket\Connection; use Icicle\WebSocket\Message; class MyApplication implements Application { public function onHandshake(Response $response, Request $request, Socket $socket): Generator { yield $response; } public function onConnection(Connection $connection): Generator { $iterator = $connection->read()->getIterator(); while (yield $iterator->isValid()) { // $iterator->getCurrent() + $connection->send(new Message()) } $connection->close(); } }

Slide 32

Slide 32 text

use Icicle\Http\Message\BasicResponse; use Icicle\Http\Message\Request; use Icicle\Http\Server\RequestHandler; use Icicle\Socket\Socket; use Icicle\WebSocket\Application; use Icicle\WebSocket\Server\Server; class MyRequestHandler implements RequestHandler { private $application; public function __construct(Application $application) { $this->application = $application; } public function onRequest(Request $request, Socket $socket): Generator { yield $this->application; } public function onError($code, Socket $socket): Generator { yield new BasicResponse($code); } } $server = new Server(new MyRequestHandler(new MyApplication()));

Slide 33

Slide 33 text

1. corou'nes 2. promises + observables 3. streams 4. filesystem 5. dns 6. threading + forking

Slide 34

Slide 34 text

icicle.io/docs

Slide 35

Slide 35 text

examples

Slide 36

Slide 36 text

github.com/asyncphp

Slide 37

Slide 37 text

why [i think] you should try async

Slide 38

Slide 38 text

value for you

Slide 39

Slide 39 text

value for the community

Slide 40

Slide 40 text

I don't get why people can't deal with async environments. Cause, for me, it's like the dream. — @dead_lugosi

Slide 41

Slide 41 text

want to know more about this stuff?

Slide 42

Slide 42 text

assertchris.io/asyncphp

Slide 43

Slide 43 text

If I said to you in 2000 that one day PHP would have any kind of strict mode, you would have laughed in my face.

Slide 44

Slide 44 text

It isn't the year 2000 any more ... it's nature is, to a large degree, dictated by RFCs that pass the vote ...

Slide 45

Slide 45 text

And while you will hear people use it's nature to argue against certain features, those voices aren't enough to stand in the way of progress. — @krakjoe

Slide 46

Slide 46 text

joind.in/talk/84ad6

Slide 47

Slide 47 text

thanks! twi$er.com/assertchris