Re: Pass variable to pcntl_fork child

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2014-07-03 10:05 GMT+02:00 Uggla Henrik <Henrik.Uggla@xxxxxxxxxxxxxxx>:

> This is a bit over my head. Could you explain more or give me some links?
>

If you have a stream, like a file-, or a socket-stream, usually when you
read from it, the process blocks until the system has read EOF
(end-of-file), or the X bytes you requested. In non-blocking mode it does
not block, but return with whatever is there, even if there is nothing ("0
byte"). The technically part is, that the OS writes everything into a
read-buffer first.

Practically this means, that you open a socket to the remote host, send
your request ("write"), then do something else, in between you read what is
already in the buffer, than do something else (and so on), and at the end,
when you receive the EOF ("fread" returns null afair), you can evaluate the
response.

Or even easier: Use a client library like Guzzle :) Don't reinvent the
wheel ;)


>
> /H
>
> ________________________________
> Från: louis [smallbanglouis@xxxxxxxxx]
> Skickat: den 3 juli 2014 09:55
> Till: Sebastian Krebs
> Kopia: Uggla Henrik; php-general@xxxxxxxxxxxxx
> Ämne: Re:  Pass variable to pcntl_fork child
>
> well right! Just forgot to use stream_set_blocking(..., ...) to set a
> stream non-blocking mode.
>
> 于 2014年07月03日 15:35, Sebastian Krebs 写道:
>
>
>
> 2014-07-03 9:26 GMT+02:00 louis <smallbanglouis@xxxxxxxxx<mailto:
> smallbanglouis@xxxxxxxxx>>:
> It's easy, you write a rest API to do the write staff, and in your main
> code , you just have to request the rest API and do not wait it to return
>
> I don't know, if I get you right
>
> - If you _provide_ an API. In that case the request should actually handle
> the request only and therefore there is no need to free the process from
> the burden to answer the request.
> - If you _consume an API, than I'd recommend using non-blocking streams
> instead. There are much easier to handle.
>
>
> 于 2014年07月03日 15:18, Uggla Henrik 写道:
> But how should I do in the HTTP SERVER environment? I don't want php to
> wait until the file write is finished before the url is displayed. I've
> tried to use flush, ob_flush but that didn't work, possibly because I have
> session_start in the beginning of my code. Should I use exec to start a
> second php?
>
> /H
> ________________________________________
> Från: louis [smallbanglouis@xxxxxxxxx<mailto:smallbanglouis@xxxxxxxxx>]
> Skickat: den 3 juli 2014 05:45
> Till: php-general@xxxxxxxxxxxxx<mailto:php-general@xxxxxxxxxxxxx>
> Ämne: Re:  Pass variable to pcntl_fork child
>
>
> Child process should has the same environment as the parent process. I
> don't see any thing wrong with your code.
> I assume that you are using php to handle http request for the using of
> $_GET , but I think it is wrong to use multi process in PHP in the HTTP
> SERVER environment
>
> 于 2014年07月02日 17:40, Uggla Henrik 写道:
> Hi!
>
> I've tried the following code but the $call variable is not available in
> the pcntl_fork child. Searching the web, I've found posts that claim that
> all variables defined before pcntl_fork are copied to the child. I've also
> found posts that claim that no variables are passed/copied to the child.
> So, which of them is true and how should I correct my code to make it work?
>
> regards
> Henrik
>
>
>
>       $call = str_replace('@@', '&', $_GET['call']);
>
>       if ($pid = pcntl_fork())
>       {
>           $previousCalls=file("calls.txt", FILE_IGNORE_NEW_LINES |
> FILE_SKIP_EMPTY_LINES);
>           if (!in_array($call, $previousCalls))
>           {
>               file_put_contents("calls.txt", $call."\n", FILE_APPEND);
>           }
>       }
>       else
>       {
>           function displayUrl($url)
>           {
>               $ch = curl_init($url);
>               curl_setopt ( $ch, CURLOPT_HEADER, 1);
>               curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
>               $curlResult = curl_exec($ch);
>               curl_close($ch);
>               list($headers,$content) = explode("\r\n\r\n",$curlResult,2);
>               foreach (explode("\r\n",$headers) as $hdr)
>               {
>                   if ($hdr != 'Transfer-Encoding: chunked')
>                   {
>                       header($hdr);
>                   }
>               }
>               echo $content;
>           }
>           displayUrl($call);
>       }
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>
>
> --
> github.com/KingCrunch <https://github.com/KingCrunch>
>
>


-- 
github.com/KingCrunch

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux