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

标签 php multithreading

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

这样做的问题是,当 PHP 代码完成执行 PHP 实例时,PHP 实例仍保留在内存中,因为无法从 PHP 中杀死它。因此,如果您正在模拟多个线程,您可以想象会发生什么。所以我仍在寻找一种可以在 PHP 中有效地完成或模拟多线程的方法。有什么想法吗?

最佳答案

多线程在 php 中是可能的

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

来自 the PHP documentation :

pthreads is an object-orientated API that provides all of the tools needed for multi-threading in PHP. PHP applications can create, read, write, execute and synchronize with Threads, Workers and Threaded objects.

Warning: The pthreads extension cannot be used in a web server environment. Threading in PHP should therefore remain to CLI-based applications only.

简单测试

#!/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

现实世界的例子

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);
}

关于php - 如何在 PHP 应用程序中使用多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70855/

相关文章:

php - yii2:在链接()之前验证()

php - PDO mysql : How to know if insert was successful

android - 在 Asynctask 中执行线程及其并发症

当我运行另一个线程时,JavaFx 进度指示器卡住

php - 如何删除codeigniter中的特定行?

php - 如何使用 PHP 列出 MySQL 中的 blob 文件?

php - 从 AppEngine 中的 Laravel PHP 连接时出现 SQL 错误 'No such file or directory'

multithreading - Delphi 线程 - 代码的哪些部分需要保护/同步?

java - Java线程共享静态变量

c++ - 线程安全的 C++ 堆栈