开发者

php stop included fle while continue with the loop in actual file?

i have lets say 2 files lets say one index.php and other one included.php

index.php

<?php
include 'included.php';
while(true)
{
     $i++;
     sleep(3);
     $o = new tester();
}
?>

included.php

<?php
class tester{
public fun开发者_StackOverflow社区ction __construct() {
//execute something
//wait for lets say one minute without stoping the flow of main loop.
//Rest code
}

?>

What i want is dont stop loop in index.php and when the tester class is provoked execute the first part and then wait for as i say one minute ?

Possible ? or do i need something else ?


You cannot do this with PHP. It's not a multi-threaded language. You can vaguely simulate it by executing another copy of PHP in parallel ,but it's not true threading - it's two completely separate processes that happen to be executing the same script.

You cannot have two or more different parts of a script executing at the same time in a single instance of PHP.


the question was very general but i don't think this is the right approach using php... usuaily php tries to execute things as quickly as possible for example rendering webpages. If you want to add timing and dynamic stuff like that would would have to send individual requests to the server every time you want to execute the script.

perhaps CRONJOBS or AJAX would be more appropriate for what you are trying to do.


You could write a new script testerStart.php which you access via url. That way you actually call another script on the server which will run idenpendently of your inital script.

So index.php would look like:

<?php
  while(true)
  {
     $i++;
     sleep(3);
     // call testerStart.php asynch
  }
?>
  • see http://petewarden.typepad.com/.../how-to-post-an.html for details
  • found this in https://stackoverflow.com/.../how-do-i-make-an-asynchronous-get-request-in-php

testerStart.php would look like:

<?php
  include 'included.php';
  $o = new tester();
?>


You may want to look some the PCNTL functions in PHP. I don't have too much experience myself with it, but stumbled across it some time ago. It's not true threading by any means, but may bring you closer to what it sounds like you're trying to achieve.

Also if you have a PHPClasses account, a link I found may be relevant to you: a PHP PCNTL wrapper class.


I'm not sure, but you can try something like

<?php
include 'included.php';
$instances = array();
while(true)
{
    foreach ($instances as $key => $instance) {
        if ($result = stream_get_contents($instance)) {
            pclose($instance);
            unset($instances[$key]);
            $o = unserialize($result);
            // do something with the object
        }
    }
    $instances[] = popen('php /path/to/include.php', 'r');
}
?>


<!-- in include.php -->
<?php
include 'included.php';
echo serialize(new tester());
?>

Or you can move all logic to "include.php" so the main script doesn't care about result at all, you will need to return just anything so that the main script knew that it may close the process handle.

Here we launch a script in separate process with popen('php /path/to/include.php', 'r'); and get a readable pipe to it (so we can read what that process outputs). When the object is successfully created in that separate process we serialize and output the result. Back in the main script with every iteration we check on already opened instances and if some of them returns something - we treat those contents as serialized object, unserialize it and then do with it whatever we want; then after doing what we wanted we close that process and remove it from opened instances list. Actually if that while(true) in your code doesn't really run forever but rather until certain condition is met - it would be a good idea to move the processing part to separate cycle, after the initialization... LIke

while($condition)
{
    $instances[] = popen('php /path/to/include.php', 'r');
}
while ( !empty($instances) ) {
    foreach ($instances as $key => $instance) {
        if ($result = stream_get_contents($instance)) {
            pclose($instance);
            unset($instances[$key]);
            $o = unserialize($result);
            // do something with the object
        }
    }
}


You'll indeed need a more complex setup.

You may indeed want to take a look at the pcntl functions as already mentioned, though these are only advised when using cli. When you need it in a web-app over Apache, you'll need to take a look at other solutions, because forking your process will not work.

Other solutions like the aforementioned popen are another good solution, though it may make your code dependent from your architecture (paths on unix are different from paths on windows, you'll need to be certain that 'php' is callable, ...)

If both of the above possibilities are no option, I'd suggest doing an async http request using either curl of fsockopen with stream_set_blocking disabled. It'll be quite a lot of work however, but the async call will get the background script running and your parent script can simply continue. Then inside your loop in the parent process, occasionally check if your background script has already been completed - with fsockopen: stream_get_contents($resource) until feof($resource)


I researched more and used my mind and finnaly i found out a way myself i did this :

index.php

<?php
$cmd = "php -q nah.php";
exec($cmd);
while (true)
{
sleep(1);
echo "lalal";
}
?>

included.php

<?php
echo "yaya";
sleep(3);
?>

And this worked as a charm

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜