Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Async PHP (Sunshine)

Async PHP (Sunshine)

PHP has emerged from it’s dark past; just in time to learn from the advances in event-based programming languages/platforms. As a result; there is vast, untapped potential in developing event-based, real-time applications. Utilising emerging open-source projects, PHP developers can join the party.

Christopher Pitt

February 06, 2016
Tweet

More Decks by Christopher Pitt

Other Decks in Programming

Transcript

  1. $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); }
  2. 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("*")); } }
  3. $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);
  4. $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();
  5. $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();
  6. $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 } }
  7. 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); } }
  8. 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; } }
  9. 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(); } }
  10. 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()));
  11. I don't get why people can't deal with async environments.

    Cause, for me, it's like the dream. — @dead_lugosi
  12. 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.
  13. It isn't the year 2000 any more ... it's nature

    is, to a large degree, dictated by RFCs that pass the vote ...
  14. 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