首页 文章

如何在PHP应用程序中使用多线程

提问于
浏览
341

有没有一种在PHP中实现多线程模型的现实方法,无论是真实的,还是仅仅模拟它 . 一段时间后,有人建议您可以强制操作系统加载另一个PHP可执行文件实例并处理其他同步进程 .

这个问题是当PHP代码完成执行时,PHP实例仍然在内存中,因为没有办法从PHP中删除它 . 因此,如果您正在模拟几个线程,您可以想象会发生什么 . 所以我仍然在寻找一种可以在PHP中有效地完成或模拟多线程的方法 . 有任何想法吗?

18 回答

  • 8

    pcntl_fork怎么样?

    查看我们的手册页以获取示例:PHP pcntl_fork

  • 2

    由于PECLpthreads≥2.0.0,Thread class可用 .

  • 3

    如果已启用安全模式, pcntl_fork 将无法在Web服务器环境中运行 . 在这种情况下,它只能在PHP的CLI版本中运行 .

  • 2

    你可以模拟线程 . PHP可以通过popen(或proc_open)运行后台进程 . 这些过程可以通过stdin和stdout进行通信 . 当然这些进程本身可以是一个php程序 . 这可能就像你得到的那样接近 .

  • -3

    对于使用GuzzleHttp进行HTTP多线程处理(因为pthreads可能无法在Web应用程序中使用),请参阅下面的博客 - https://blog.madewithlove.be/post/concurrent-http-requests/

  • 0

    您可以使用exec()来运行命令行脚本(例如命令行php),如果将输出传递给文件,那么您的脚本将不会等待命令完成 .

    我不太记得php CLI语法,但你想要的东西如下:

    exec("/path/to/php -f '/path/to/file.php' | '/path/to/output.txt'");
    

    我认为出于安全原因,默认情况下会禁用多个共享托管服务器exec(),但可能值得一试 .

  • 3

    根据您的尝试,您还可以使用curl_multi来实现它 .

  • 2

    我知道这已经过时了,但你可以看看http://phpthreadlib.sourceforge.net/

    它支持双向线程间通信,并具有内置保护功能,可以消除子线程(防止孤儿) .

  • 5

    您可以选择:

    • multi_curl

    • 可以使用系统命令

    • 理想情况是,用C语言创建线程函数并在PHP中编译/配置 . 现在该功能将是PHP的功能 .

  • 368

    虽然你不能线程,但你确实在php中有一定程度的进程控制 . 这里有用的两个函数集是:

    过程控制功能http://www.php.net/manual/en/ref.pcntl.php

    POSIX函数http://www.php.net/manual/en/ref.posix.php

    您可以使用pcntl_fork分叉您的进程 - 返回子进程的PID . 然后你可以使用posix_kill来解析那个PID .

    也就是说,如果你杀死一个父进程,应该将一个信号发送给子进程,告诉它死掉 . 如果php本身没有意识到这一点,你可以注册一个函数来管理它,并使用pcntl_signal做一个干净的退出 .

  • 15

    在PHP中可以实现多线程

    是的,您可以使用pthreads在PHP中进行多线程处理

    来自the PHP documentation

    pthreads是一个面向对象的API,它提供了PHP中多线程所需的所有工具 . PHP应用程序可以创建,读取,写入,执行和与Threads,Workers和Threaded对象同步 . 警告:pthreads扩展名不能在Web服务器环境中使用 . 因此,PHP中的线程应保留在基于CLI的应用程序中 .

    Simple Test

    #!/usr/bin/php
    <?php
    class AsyncOperation extends Thread {
    
        public function __construct($arg) {
            $this->arg = $arg;
        }
    
        public function run() {
            if ($this->arg) {
                $sleep = mt_rand(1, 10);
                printf('%s: %s  -start -sleeps %d' . "\n", date("g:i:sa"), $this->arg, $sleep);
                sleep($sleep);
                printf('%s: %s  -finish' . "\n", date("g:i:sa"), $this->arg);
            }
        }
    }
    
    // Create a array
    $stack = array();
    
    //Initiate Multiple Thread
    foreach ( range("A", "D") as $i ) {
        $stack[] = new AsyncOperation($i);
    }
    
    // Start The Threads
    foreach ( $stack as $t ) {
        $t->start();
    }
    
    ?>
    

    第一次运行

    12:00:06pm:     A  -start -sleeps 5
    12:00:06pm:     B  -start -sleeps 3
    12:00:06pm:     C  -start -sleeps 10
    12:00:06pm:     D  -start -sleeps 2
    12:00:08pm:     D  -finish
    12:00:09pm:     B  -finish
    12:00:11pm:     A  -finish
    12:00:16pm:     C  -finish
    

    第二轮

    12:01:36pm:     A  -start -sleeps 6
    12:01:36pm:     B  -start -sleeps 1
    12:01:36pm:     C  -start -sleeps 2
    12:01:36pm:     D  -start -sleeps 1
    12:01:37pm:     B  -finish
    12:01:37pm:     D  -finish
    12:01:38pm:     C  -finish
    12:01:42pm:     A  -finish
    

    Real World Example

    error_reporting(E_ALL);
    class AsyncWebRequest extends Thread {
        public $url;
        public $data;
    
        public function __construct($url) {
            $this->url = $url;
        }
    
        public function run() {
            if (($url = $this->url)) {
                /*
                 * If a large amount of data is being requested, you might want to
                 * fsockopen and read using usleep in between reads
                 */
                $this->data = file_get_contents($url);
            } else
                printf("Thread #%lu was not provided a URL\n", $this->getThreadId());
        }
    }
    
    $t = microtime(true);
    $g = new AsyncWebRequest(sprintf("http://www.google.com/?q=%s", rand() * 10));
    /* starting synchronization */
    if ($g->start()) {
        printf("Request took %f seconds to start ", microtime(true) - $t);
        while ( $g->isRunning() ) {
            echo ".";
            usleep(100);
        }
        if ($g->join()) {
            printf(" and %f seconds to finish receiving %d bytes\n", microtime(true) - $t, strlen($g->data));
        } else
            printf(" and %f seconds to finish, request failed\n", microtime(true) - $t);
    }
    
  • 28

    你为什么不用popen

    for ($i=0; $i<10; $i++) {
        // open ten processes
        for ($j=0; $j<10; $j++) {
            $pipe[$j] = popen('script2.php', 'w');
        }
    
        // wait for them to finish
        for ($j=0; $j<10; ++$j) {
            pclose($pipe[$j]);
        }
    }
    
  • 10

    PHP中没有线程化,但是通过将HTTP请求用作异步调用,可以进行并发编程 .

    将curl的超时设置设置为1并对要相互关联的进程使用相同的session_id,可以与会话变量进行通信,如下例所示 . 使用此方法,您甚至可以关闭浏览器,并且服务器上仍然存在并发进程 .

    不要忘记验证正确的会话ID,如下所示:

    http://localhost/test/verifysession.php?sessionid = [正确的ID]

    startprocess.php

    $request = "http://localhost/test/process1.php?sessionid=".$_REQUEST["PHPSESSID"];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $request);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    curl_exec($ch);
    curl_close($ch);
    echo $_REQUEST["PHPSESSID"];
    

    process1.php

    set_time_limit(0);
    
    if ($_REQUEST["sessionid"])
       session_id($_REQUEST["sessionid"]);
    
    function checkclose()
    {
       global $_SESSION;
       if ($_SESSION["closesession"])
       {
           unset($_SESSION["closesession"]);
           die();
       }
    }
    
    while(!$close)
    {
       session_start();
       $_SESSION["test"] = rand();
       checkclose();
       session_write_close();
       sleep(5);
    }
    

    verifysession.php

    if ($_REQUEST["sessionid"])
        session_id($_REQUEST["sessionid"]);
    
    session_start();
    var_dump($_SESSION);
    

    closeprocess.php

    if ($_REQUEST["sessionid"])
        session_id($_REQUEST["sessionid"]);
    
    session_start();
    $_SESSION["closesession"] = true;
    var_dump($_SESSION);
    
  • 2

    通过pthreads PECL扩展可以使用线程

    http://www.php.net/manual/en/book.pthreads.php

  • 8

    我知道这是一个老问题,但对于搜索的人来说,有一个用C语言编写的PECL扩展,它现在提供PHP多线程功能,它位于这里https://github.com/krakjoe/pthreads

  • 4

    在撰写我当前的评论时,我不知道PHP线程 . 我自己来找这里的答案,但一个解决方法是从Web服务器接收请求的PHP程序将整个答案公式委托给控制台应用程序,该应用程序将其输出(请求的答案)存储到二进制文件中并且启动控制台应用程序的PHP程序将逐字节返回该二进制文件作为接收请求的答案 . 控制台应用程序可以用在服务器上运行的任何编程语言编写,包括那些具有适当线程支持的编程语言,包括使用OpenMP的C程序 .

    一个不可靠,肮脏的技巧是使用PHP执行控制台应用程序,“uname”,

    uname -a
    

    并将该控制台命令的输出打印到HTML输出,以找出服务器软件的确切版本 . 然后将完全相同版本的软件安装到VirtualBox实例,编译/组装任何完全自包含的,最好是静态的二进制文件,然后将其上传到服务器 . 从那时起,PHP应用程序可以在具有适当多线程的控制台应用程序的角色中使用这些二进制文件 . 当服务器管理员尚未将所有需要的编程语言实现安装到服务器时,这是一种肮脏,不可靠的解决方法 . 需要注意的是,PHP应用程序收到控制台应用程序的每个请求都会终止/退出/ get_killed .

    至于主机服务管理员对这种服务器使用模式的看法,我想这归结为文化 . 在北欧,服务提供商必须提供广告,如果允许执行控制台命令并允许上传非恶意软件文件,服务提供商有权在几分钟甚至30秒后终止任何服务器进程,那么托管服务管理员缺乏形成适当投诉的任何论据 . 在美国和西欧,情况/文化非常不同,我相信在美国和/或西欧,托管服务提供商很可能会拒绝为使用上述技巧的托管服务客户提供服务 . 这只是我的猜测,考虑到我个人对美国托管服务的体验,以及我从其他人那里听到的有关西欧托管服务的消息 . 在撰写我目前的评论(2018_09_01)时,我对南欧主机服务提供商,南欧网络管理员的文化规范一无所知 .

  • -2

    可能是我错过了一些东西但是exec在windows环境中没有像我一样异步我在windows中使用以下它就像魅力一样;)

    $script_exec = "c:/php/php.exe c:/path/my_ascyn_script.php";
    
    pclose(popen("start /B ". $script_exec, "r"));
    
  • 0

    多线程意味着同时执行多个任务或进程,我们可以通过使用以下代码在php中实现这一点,虽然没有直接的方法在php中实现多线程,但我们可以通过以下方式实现几乎相同的结果 .

    chdir(dirname(__FILE__));  //if you want to run this file as cron job
     for ($i = 0; $i < 2; $i += 1){
     exec("php test_1.php $i > test.txt &");
     //this will execute test_1.php and will leave this process executing in the background and will go         
    
     //to next iteration of the loop immediately without waiting the completion of the script in the   
    
     //test_1.php , $i  is passed as argument .
    

    }

    Test_1.php

    $conn=mysql_connect($host,$user,$pass);
    $db=mysql_select_db($db);
    $i = $argv[1];  //this is the argument passed from index.php file
    for($j = 0;$j<5000; $j ++)
    {
    mysql_query("insert  into  test   set
    
                    id='$i',
    
                    comment='test',
    
                    datetime=NOW() ");
    
    }
    

    这将同时执行test_1.php两次,并且两个进程将同时在后台运行,因此这样你就可以在php中实现多线程 .

    这家伙做得很好Multithreading in php

相关问题