RT  
或者说是模拟多线程来运行多个脚本现在我有a.php,b.php,c.php三个脚本(都比较耗时!),我需要这三个能够同时运行,并且互不干扰,运行所需要的参数由外部提供,请大家帮帮忙吧~   ps:如果能够在线程运行当中还能获取运行状态 那就更好啦~~

解决方案 »

  1.   

    有人说用socket,不过我一般都是用ajax
      

  2.   

    方法一:  jquery load(a.php);
    load(b.php);
    load(c.php);方法二:  php  curl
    connomains = array(
       "http://www.cnn.com/",
       "http://www.canada.com/",
       "http://www.yahoo.com/"
       );$mh = curl_multi_init();foreach ($connomains as $i => $url) {
           $conn[$i]=curl_init($url);
           curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,1);
           curl_multi_add_handle ($mh,$conn[$i]);
    }do { $n=curl_multi_exec($mh,$active); } while ($active);foreach ($connomains as $i => $url) {
           $res[$i]=curl_multi_getcontent($conn[$i]);
           curl_close($conn[$i]);
    }
    方法二  可以写一个php  同时请求一个地址 文件  根据后面才参数的不同调用不同的文件  实现多进程  多线程的方法暂时  没着落~~~
      

  3.   

    jquery跟ajax不都是用在页面上的吗? 我这个现在跟页面可以说已经没有关系了~就是单纯的要在本地运行,而且,还不能阻塞主程序的运行
     那个curl的方法貌似还有希望,但是 我试过了,这样会影响后边程序的运行的,就阻塞到第一次运行的耗时的代码上了啊…… 下面是我用fsockopen 模拟的多线程, 但是  ,其中一个比较耗时的下载文件方法, 每次文件都不能完整的下载,一共40多M的文件 顶多只能下载4、5M 就不再下载了。
    请大家看看是什么原因呢??
    class thread
    {
        var $hooks = array();
        var $args = array();
        function thread ()
        {}
        function addthread ($func)
        {
            $args = array_slice(func_get_args(), 1);
            $this->hooks[] = $func;
            $this->args[] = $args;
            return true;
        }
        function runthread ()
        {
            if (isset($_GET['flag'])) {
                $flag = intval($_GET['flag']);
            }
            if ($flag || $flag === 0) {
                call_user_func_array($this->hooks[$flag], $this->args[$flag]);
            } else {
                for ($i = 0, $size = count($this->hooks); $i < $size; $i ++) {
                    $fp = fsockopen($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'],$error,$errors,3000000);
                    if ($fp) {
                        $out = "GET {$_SERVER['PHP_SELF']}?flag=$i HTTP/1.1\r\n";
                        $out .= "Host: {$_SERVER['HTTP_HOST']}\r\n";
                        $out .= "Connection: Close\r\n\r\n";
                        fputs($fp, $out);
                        fclose($fp);
                    }
                }
            }
        }
    }
    $thread = new thread();
    $thread->addthread('a', 'a');
    $thread->addthread('b', 'b');
    $thread->runthread();
    function a ($info)
    {
        $log = microtime() . '.log';
        $txt = $info . "\r\n\r\n" . 'Set in ' . Date('h:i:s', time()) .
         (double) microtime() . "\r\n";
        $fp = fopen($log, 'w');
        fwrite($fp, $txt);
        fclose($fp);
        $url = "这里是大体积文件下载地址";
        $file = fopen($url, "rb");
        if ($file) {
            $newf = fopen("C:\\Users\\Echo\\Desktop\\full.zip", "wb");
            if ($newf) {
                while (! feof($file)) {
                    fwrite($newf, fread($file, 1024 * 8));
                }
            }
        }
        fclose($file);
        fclose($newf);
    }
    function b ($info)
    {
        $log = microtime() . '.log';
        $txt = $info . "\r\n\r\n" . 'Set in ' . Date('h:i:s', time()) .
         (double) microtime() . "\r\n";
        $fp = fopen($log, 'w');
        fwrite($fp, $txt);
        fclose($fp);
        $zip_filepath = "C:\\Users\\Echo\\Desktop\\test.zip";
        if (! is_file($zip_filepath)) {
            die("文件" . $zip_filepath . "不存在");
        }
        $zip = new ZipArchive();
        (bool) $rs = $zip->open($zip_filepath);
        if ($rs !== TRUE) {
            die("解压缩失败!!Error Code:" . $rs);
        }
        print_r($zip);
        $zip->extractTo("C:\\Users\\Echo\\Desktop\\");
        $zip->close();
      

  4.   

    好吧! 自己搞定啦~   模拟多线程脚本类如下:
    class thread
    {
        var $hooks = array();
        var $args = array();
        function thread ()
        {}
        function addthread ($func)
        {
            $args = array_slice(func_get_args(), 1);
            $this->hooks[] = $func;
            $this->args[] = $args;
            return true;
        }
        function runthread ()
        {
            if (isset($_GET['flag'])) {
                $flag = intval($_GET['flag']);
            }
            if ($flag || $flag === 0) {
                call_user_func_array($this->hooks[$flag], $this->args[$flag]);
            } else {
                for ($i = 0, $size = count($this->hooks); $i < $size; $i ++) {
                    $fp = fsockopen($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'],$error,$errors,3000000);
                    if ($fp) {
                        $out = "GET {$_SERVER['PHP_SELF']}?flag=$i HTTP/1.1\r\n";
                        $out .= "Host: {$_SERVER['HTTP_HOST']}\r\n";
                        $out .= "Connection: Close\r\n\r\n";
                        fputs($fp, $out);
                        fclose($fp);
                    }
                }
            }
        }
    }
    $thread = new thread();
    $thread->addthread('a', 'a');
    $thread->addthread('b', 'b');
    $thread->runthread();
    断点续传的时候 下载数据不对 是因为本地应该用 rb+打开…… 
      

  5.   

    php可多进程,也可fork子进程.如需互斥,可引入信号量
    fsockopen那个模拟说到底是web server的多线程,和具体开发语言无关。
      

  6.   


    是啊~ 我这也是没办法才这样搞的~~ 您能说说怎么真正的来多线程吗 ?
     fork子进程的方法我看过, 好像是子进程运行时候,在没有返回结果前,父进程是挂起的~ 这不符合我的需求啊~ 
      

  7.   

    我没说清楚,php语言本身目前为止是不支持多线程的。
    你可以fork 3个子进程,然后同时执行,父进程就让他挂着。
      

  8.   

    进程通讯,system V实现只属linux,windows只能用共享内存了(shmop_open)。
      

  9.   

    主要是 主进程不能让他一直挂着啊~ 他还得跑别的东西的~~  
      估计就只能有我上边这么一个招了吧?还有 大哥们有空看看我另一个帖子吧~~  一天了都没人回答……
    http://topic.csdn.net/u/20110805/10/a37da864-0a32-4377-8175-d5259869da5a.html
      

  10.   

    其实你所谓的挂起具体是什么意思?
    父进程一样可以执行东西啊
    下面是手册的例子
    $pid = pcntl_fork();
    if ($pid == -1) {
         die('could not fork');
    } else if ($pid) {
         // we are the parent,父进程
         pcntl_wait($status); //Protect against Zombie children
    } else {
         // we are the child ,子进程
    }
      

  11.   


    if ($pid) {
         // we are the parent,父进程
         pcntl_wait($status); //Protect against Zombie children
    } else {
         // we are the child ,子进程
    }

    这里父进程跟子进程不是只能有一个执行吗?
     两个没法同时执行的吧?
      

  12.   

    php命令行脚本多进程并发执行http://hi.baidu.com/xletian/blog/item/95a5389b705c78b0c9eaf49a.html这是你需要的吗?