note 82326 added to function.posix-mkfifo

From: Date: Sat, 05 Apr 2008 20:11:54 +0000
Subject: note 82326 added to function.posix-mkfifo
Groups: php.notes 
Request: Send a blank email to php-notes+get-137969@lists.php.net to get a copy of this message
Here is a possible solution to what - tech at kwur dot com- mentioned: I faced the problem where i had a process (a server) that needed to take care of socket connection, and in the meanwhile get some data from the database. I didn't wanted to make the clients wait for the query execution time, so i decided to make a separate process that executes the query on the DB, and the two would communicate over a pipe. Of course i didn't wanted the server blocking if no data was available. So what i come up with is to use stream_select() , and to overcome the mentioned problem, i would fork the process, open up the pipe for writing in the child, this way the parent won't block when it opens the pipe. here is some code <code> <?php if (pcntl_fork() == 0) { // kid $file = fopen("JobsQueue","w"); sleep(1); exit(0); } else { usleep(15); // make sure that the child executes first $file = fopen("JobsQueue","r"); } echo "opened the queue \n"; while (true) { $reders = array($file); if (stream_select($reders,$writers=null,$except=null,0,15) < 1) { continue; } else { // read data from the fifo $data = fread($file,1024); echo $data; } sleep(1); } ?> </code> ---- Server IP: 216.92.131.65 Probable Submitter: 79.116.68.207 ---- Manual Page -- http://www.php.net/manual/en/function.posix-mkfifo.php Edit -- https://master.php.net/note/edit/82326 Del: integrated -- https://master.php.net/note/delete/82326/integrated Del: useless -- https://master.php.net/note/delete/82326/useless Del: bad code -- https://master.php.net/note/delete/82326/bad+code Del: spam -- https://master.php.net/note/delete/82326/spam Del: non-english -- https://master.php.net/note/delete/82326/non-english Del: in docs -- https://master.php.net/note/delete/82326/in+docs Del: other reasons-- https://master.php.net/note/delete/82326 Reject -- https://master.php.net/note/reject/82326 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#137969) next »