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

Piping into PHP

Piping into PHP

Piping into PHP
Not the kind of pipe you smoke :) Though traditionally used to build websites, PHP has a great deal of often unused functionality. In this talk we will explore Unix named pipes and how we can start a PHP process which listens for input while it is running.

Shaun Morrow

May 26, 2015
Tweet

More Decks by Shaun Morrow

Other Decks in Programming

Transcript

  1. WHAT „ Send input to running PHP process „ Use named pipes

    which are an extension of … „ Pipelines which make use of … „ Streams
  2. WHY „ Long running CLI PHP script „ No control once it

    is running „ No visibility on process „ Growing needs
  3. WHAT ARE STREAMS? „ Data elements processed in order „ 3 standard

    streams „  Input - stdin „  Output - stdout „  Error - stdout
  4. PIPELINES „ Processes chained by standard streams „ Output (stdout) fed into

    next process (stdin) „ Errors (stderr) displayed on calling terminal
  5. REDIRECTION „ Output and errors can be redirected „ Redirecting output to

    another program is how pipelines are built „ Can also redirect to a file
  6. MULTI USE PIPES „ Traditional pipes exist anonymously „ Only exist as

    long as the process is running „ Named pipes persist while system is running „ Can be used more than once „ Can be deleted when no longer needed
  7. WORKING WITH NAMED PIPES „ Created with `mkfifo` command „ Get the

    pipe ready for input „ Fire off a process to read from the pipe „ Write something to the pipe
  8. READ FROM A STREAM „ Can access the input stream using

    an inbuilt wrapper „ STDIN constant is recommended
  9. WHAT ITS DOING „ First set the stream to non blocking

    „ Loop over the stream „ Read from the stream „ End of line? Break out
  10. BUT WILL IT STOP? „ We saw it reads from stdin

    „ Lets see if we can get it to exit on demand
  11. WE SPOKE ABOUT „ What and why „ Streams „ Unix pipelines „ Named

    pipes „ Getting PHP to listen for input „ Summary
  12. SOURCES „  "Stdstreams-notitle" by ScotXW - Own work. Licensed under

    Public Domain via Wikimedia Commons - http://commons.wikimedia.org/wiki/File:Stdstreams-notitle.svg#/media/ File:Stdstreams-notitle.svg „  "Pipeline" by TyIzaeL - en:File:Pipeline.svg. Licensed under Public Domain via Wikimedia Commons - http://commons.wikimedia.org/wiki/File:Pipeline.svg#/media/ File:Pipeline.svg „  “Standard streams” – Wikipedia - http://en.wikipedia.org/wiki/Standard_streams „  “Pipeline” –Wikipedia - http://en.wikipedia.org/wiki/Pipeline_(Unix) „  “Named pipe” – Wikipedia - http://en.wikipedia.org/wiki/Named_pipe
  13. SOURCES CONTD. „  “Input/output streams” – php.net - http://php.net/manual/en/features.commandline.io-streams.php „ 

    “stream_set_blocking” – php.net - http://php.net/manual/en/function.stream-set-blocking.php „  “Introduction to Named Pipes” – LINUX JOURNAL - http://www.linuxjournal.com/article/2156