gpt4 book ai didi

PHP pthreads 工作起来很奇怪

转载 作者:行者123 更新时间:2023-12-03 12:55:56 24 4
gpt4 key购买 nike

对不起我扭曲的英语 - 它不是我的母语。

我有一个麻烦:pthreads 可以工作,但是以某种奇怪的方式:例如,我不能从线程中“回显”。
来自互联网的一些示例代码:

<?php
ini_set('display_errors',E_ALL);
class hashThread extends Thread {
private $max = 1;
private $index = 0;
function __construct($max, $index)
{
$this->max = $max;
$this->index = $index;
}
public function run()
{
for ($i=1; $i<=$this->max; $i++)
{
md5($i);
}
error_log("Thread #{$this->index} finished\r\n");
echo "Thread #{$this->index} finished\r\n";
}
}
$thread_count = 8;
$start_time = microtime(true);
for($i=1; $i<=$thread_count; $i++)
{
$thread[$i] = new hashThread(1e6, $i);
$thread[$i]->start(PTHREADS_INHERIT_NONE);
}
echo "Done in: " . round(microtime(true) - $start_time, 2) . " seconds";
?>

脚本仅显示“完成:0.14 秒”(在网页中),没有 echo ,但是!它在错误日志中:

$ tail -f/var/log/nginx/error_log | grep 线程
2014/03/07 08:05:10 [错误] 12621#0: *2818394 FastCGI 在标准错误中发送:“PHP 消息:线程 #2 完成” ...
2014/03/07 08:05:10 [错误] 12621#0: *2818394 FastCGI 在标准错误中发送:“PHP 消息:线程 #1 完成” ...
2014/03/07 08:05:10 [错误] 12621#0: *2818394 FastCGI 在标准错误中发送:“PHP 消息:线程 #3 完成” ...
2014/03/07 08:05:10 [错误] 12621#0: *2818394 FastCGI 在标准错误中发送:“PHP 消息:线程 #6 完成” ...
2014/03/07 08:05:10 [错误] 12621#0: *2818394 FastCGI 在标准错误中发送:“PHP 消息:线程 #5 完成” ...
2014/03/07 08:05:10 [错误] 12621#0: *2818394 FastCGI 在标准错误中发送:“PHP 消息:线程 #7 完成” ...
2014/03/07 08:05:10 [错误] 12621#0: *2818394 FastCGI 在标准错误中发送:“PHP 消息:线程 #4 完成” ...
2014/03/07 08:05:10 [错误] 12621#0: *2818394 FastCGI 在标准错误中发送:“PHP 消息:线程 #8 完成” ...

事实上,我的线程中没有使用任何回显,我需要聚合一些 Web 服务(每个 Web 服务类 = 线程),等待最慢的线程,并将所有结果合并到一个可以从外部获取的数组中,但是我以前从未使用过 PHP 或其他语言的线程。如果你能给我一些例子,我将不胜感激。谢谢你。

软件:
Gentoo x86
PHP 5.4.23(nginx 配置为使用 php-fpm)
Nginx 1.4.4

没有 Apache 。 PHP 内置线程安全,启用 pthreads 模块。

最佳答案

无法保证标准输出,Zend 没有提供 API 来确保您获得正确的流,它不能因为它工作的所有环境,它留给您堆栈中 PHP 之上的软件,FPM 确实算作堆栈中 PHP 之上,它对 stderr/stdout 做了一些奇怪的事情——它必须这样做才能工作。

所以,不要那样做!

如前所述,无论如何都不需要编写标准输出,如果要记录,则为文件编写记录器:)

这是您的起点:

  • https://github.com/krakjoe/pthreads/blob/master/README.md

  • 阅读该自述文件的文档部分中的文章。第一部分准确地解释了 pthreads 如何存在以及它是如何工作的,并将为您提供良好的知识基础。

    第二个描述了我们现在拥有的最高级别的线程,即池,你可以跳过那个......我不会:)

    请做阅读,这将有很大的帮助......

    当您阅读和理解时,这里有一个半相关的示例代码 list ,它从几个位置获取页面并将结果存储在一个共享数组中,然后主线程可以处理该数组:
    <?php
    /*
    Just a container class, nothing to see here
    */
    class Store extends Stackable {
    public function run(){}
    }

    class Hash extends Thread {
    /*
    * Take shared storage and target for fetching
    */
    public function __construct(Store $store, $target) {
    $this->store = $store;
    $this->target = $target;
    }

    /*
    * Fetch contents of target and store associatively in Store
    */
    public function run() {
    $this->store[$this->target] = file_get_contents(
    $this->target);
    }

    protected $store;
    protected $target;
    }

    /* will hold shared data */
    $store = new Store();
    /* will hold threads */
    $threads = [];
    $thread = 0;

    /* create and start all threads */
    while ($thread < 4) {
    $threads[$thread] = new Hash($store, sprintf(
    "http://www.google.co.uk/?q=%s", md5(mt_rand()*microtime())));
    $threads[$thread]->start();
    $thread++;
    }

    /* join all threads */
    foreach ($threads as $thread)
    $thread->join();

    foreach ($store as $target => $data)
    printf("%s returned %d bytes\n", $target, strlen($data));
    ?>

    当你到达这里时,你应该已经阅读了我写的关于 pthreads 的每一个字,希望 :)

    您现在已经准备好查看您正在进行的事件类型的更复杂但更相关的示例,以下是在 SO 上为其他人编写的一些代码,它使用 DOM/XPath 从 phpdocs 获取文档描述......

    https://gist.github.com/krakjoe/b1526fcc828621e840cb

    我认为这应该足够一天的代码和文档了,对吧??

    关于PHP pthreads 工作起来很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22244972/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com