Re: [PHP] Exec Script in the background, don't wait till it finishes
On Jan 14, 2011, at 10:56 AM, Kai Renz wrote: Hi there, i'm currently working on a socket project. It consists of two scripts. The first script (socket1.php) creates a socket on a specified port and waits for a client to connect. If a client connects, they exchange some informations, including a random created port. Next, the first script calls the second script (socket2.php) with the just created port number as a parameter. Now here is the problem: it all works fine, but the client can't send a quit message to socket1 because socket1.php waits until socket2.php is finished. socket2.php is working properly, and if i connect to the new socket and quit the connection, socket1 is working again. All i need socket1 to do is to continue working independant of socket2. I tried everything, from shell_exec to pcntl_exec, nothing seems to work. regards, Kai -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php To the best of my knowledge you are kind of in a hard place with that type of situation. I hate to be the guy that in the last three weeks mentions using a different language for an issue, but I honestly think that you may have a higher return using a language that supports threads a bit better. Hope this doesn't start another language battle. Don't get me wrong, I love PHP, but there are certain things that a different tool may be easier to solve the problem with. PHP or not: Could you describe the issue a bit more? What is the goal, at the end of the day? Here is what I am gathering: Open a socket client connects get client to a different port to run independently Can I assume that the goal of moving them to a different port is to be able to accept more clients on the first port? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Exec Script in the background, don't wait till it finishes
On Fri, Jan 14, 2011 at 11:56, Kai Renz writeme...@googlemail.com wrote: Now here is the problem: it all works fine, but the client can't send a quit message to socket1 because socket1.php waits until socket2.php is finished. socket2.php is working properly, and if i connect to the new socket and quit the connection, socket1 is working again. All i need socket1 to do is to continue working independant of socket2. I tried everything, from shell_exec to pcntl_exec, nothing seems to work. Have you looked into pcntl_fork() or running it in the background via something as simple as backticks? The latter of which, exemplified: ?php `/bin/env php /path/to/socket2.php --port 1234 /path/to/log.txt 21 `; ? Expanding on that, you could do something like this: ?php exec('/bin/env php /path/to/socket2.php --port 1234 /path/to/log.txt 21 ',$ret,$err); if ($err === 0) { exec('echo $$',$ret); $pid = $response[0]; echo $pid.PHP_EOL; } else { echo 'There was an error while forking the process. Please view the log.'.PHP_EOL; } ? -- /Daniel P. Brown Network Infrastructure Manager Documentation, Webmaster Teams http://www.php.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Exec Script in the background, don't wait till it finishes
On Fri, Jan 14, 2011 at 13:28, Kai Renz writeme...@googlemail.com wrote: (Putting this back on the list. Please use Reply-All.) @Daniel: Yeh i tried sending it to the background, this works but still it does wait for the other script to finish. It shouldn't. In fact, just to see if I somehow completely forgot how things worked, I just mocked up the following and verified it worked perfectly: ?php // 1.php `php ./2.php /dev/null 21 `; echo 'I will now terminate, while the other script continues.'.PHP_EOL; ? ?php // 2.php while (1) { file_put_contents('./123-whee.txt','I\'m still running!'.PHP_EOL,FILE_APPEND); } ? dan@blackbox:~$ php 1.php I will now terminate, while the other script continues. dan@blackbox:~$ tail -f ./123-whee.txt I'm still running! I'm still running! I'm still running! I'm still running! I'm still running! I'm still running! I'm still running! I'm still running! I'm still running! I'm still running! I'm still running! I'm still running! I'm still running! I'm still running! I'm still running! I'm still running! I'm still running! I'm still running! I'm still running! I'm still running! ^C dan@blackbox:~$ ps -ef | grep -i php dan 29428 1 34 13:37 pts/166 00:00:24 php ./2.php dan 29491 28729 0 13:38 pts/166 00:00:00 grep --color=auto -i php -- /Daniel P. Brown Network Infrastructure Manager Documentation, Webmaster Teams http://www.php.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Exec Script in the background, don't wait till it finishes
Hi guys and thanks for your answers... @Nicholas: Yes, you are right. The first socket is only used if a new clients connects, thats why the script generates a new port so the client can connect to the new socket. After that socket1 should continue its work and wait for new clients. @Daniel: Yeh i tried sending it to the background, this works but still it does wait for the other script to finish. @Evil Son: Thanks for the tip, i'll try it :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Exec Script in the background, don't wait till it finishes
On Jan 14, 2011, at 12:41 PM, Kai Renz wrote: Hi guys and thanks for your answers... @Nicholas: Yes, you are right. The first socket is only used if a new clients connects, thats why the script generates a new port so the client can connect to the new socket. After that socket1 should continue its work and wait for new clients. @Daniel: Yeh i tried sending it to the background, this works but still it does wait for the other script to finish. @Evil Son: Thanks for the tip, i'll try it :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Your not going to be able to push it to the background, it will always wait no matter where you push it. You will have to spawn another full script. The pcntl_fork() function is what you are looking for, but it works a tad less than optimal in this situation. Here is a QnD of a class and using it. This thing creates a bit of a mess, but it illustrates that the parent doesn't wait on the child to die. You will need to write some logic to determine if it is the parent or child fork that is running, and also keep track of your ports. Like I said earlier, it creates a mess, but clearly illustrates pcntl_fork(); This is a messy thing to be implementing in PHP. ?php $_pid = getmypid(); echo \n~~ Start $_pid\t; $_port = 8080; $_socket = new MySocket($_port); $_socket-open(); $_socket-handleNewClient(++$_port); $_socket-handleNewClient(++$_port); $_socket-handleNewClient(++$_port); $_socket-handleNewClient(++$_port); $_socket-handleNewClient(++$_port); echo \n~~ End $_pid\t; class MySocket { private $port; public function __construct($_port) { $this-port = getmypid(); //Use port number if ya want - I used PID for uniqueness echo \n--- Created new port $this-port\t; } public function open() { echo \nOpen port: $this-port\n; for ($_i = 0; $_i 100; $_i++) {// Do some thinking: Clog up the buffer echo [$this-port]; } } public function close() { echo \n=== Closing $this-port\t; } public function handleNewClient($_port) { $_socket = new MySocket($_port); $_socket-open(); pcntl_fork(); // Creates an exponential mess $_socket-close(); // Close socket after spawning another script } // Rest of class logic... } ?
Re: [PHP] Exec Script in the background, don't wait till it finishes
On 14 January 2011 18:48, Nicholas Kell n...@monkeyknight.com wrote: On Jan 14, 2011, at 12:41 PM, Kai Renz wrote: Hi guys and thanks for your answers... @Nicholas: Yes, you are right. The first socket is only used if a new clients connects, thats why the script generates a new port so the client can connect to the new socket. After that socket1 should continue its work and wait for new clients. @Daniel: Yeh i tried sending it to the background, this works but still it does wait for the other script to finish. @Evil Son: Thanks for the tip, i'll try it :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php One more thing to verify: What is the target environment? *nix I hope. You can launch a non-blocking process in Windows using PHP. I do this a lot. ?php $o_Shell = new COM('WScript.Shell'); $o_Shell-Run('C:\\PHP5\\php-win.exe -f script.php -- script_params_would_go_here', 0, False); // This script can now continue and script.php will be running in parallel. ? I use WinCache's Ucache to pass data between the multiple threads. Now, use the PECL extension Win32Service, and you can use PHP for Windows Services, multi-threaded with inter-thread communication. If you feel mad enough. Regards, Richard. [1] http://msdn.microsoft.com/en-us/library/d5fk67ky(v=vs.85).aspx -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php