PHP7.2 安裝 PThread

字號+ 編輯: 国内TP粉 修訂: 种花家 來源: ThinkPHP 2023-09-11 我要說兩句(1)

通過本篇,能學會如何安裝PHP7.2版本的PThread

前言:

之前想通過PHP多線程分批去處理請求,等最後安裝好,運行成功後才發現官方的一句話,最後不得不放棄使用了。

SAPI Support

pthreads v3 is restricted to operating in CLI only: I have spent many years trying to explain that threads in a web server just don't make sense, after 1,111 commits to pthreads I have realised that, my advice is going unheeded.

So I'm promoting the advice to hard and fast fact: you can't use pthreads safely and sensibly anywhere but CLI.

Thanks for listening ;)

安裝:

1,我是使用oneinstack 集成工具,安裝Thread前需要在oneinstack/options.conf 增加php編譯擴展(開啓安全模式)

php_modules_options='--enable-maintainer-zts

2,檢查是否已開啓安全線程

php -i | grep Thread

看到enable 表示是安全版

3,下載最新的Pthreads擴展

git clone https://github.com/krakjoe/pthreads.git

4,在下載好的pthreads目錄下執行phpsize命令

phpize

5,執行安裝命令

./configure --with-php-config=/usr/local/php/bin/php-config    
make && make install

6,寫入配置文档
echo "extension=pthreads.so" >> /usr/local/php/etc/php.ini

7,測試代碼

<?php
class My extends Thread
{
    public function run()
    {
        sleep(10);
        echo ("HELLO 1 \r\n");
    }
}

class My2 extends Thread
{
    public function run()
    {
        /* ... */
        sleep(3);
        echo ("HELLO 2 \r\n");
    }
}



class My3 extends Thread
{
    public function run()
    {
        /* ... */
        sleep(1);
        echo ("HELLO 3 \r\n");
    }
}


class My4 extends Thread
{
    public function run()
    {
        /* ... */
        sleep(5);
        echo ("HELLO 4 \r\n");
    }
}


$my_1 = new My();
$my_2 = new My2();
$my_3 = new My3();
$my_4 = new My4();

$my_1->start();
$my_2->start();
$my_3->start();
$my_4->start();

$my_1->join();
$my_2->join();
$my_3->join();
$my_4->join();
echo ("main ");

// 輸出結果
HELLO 3 => HELLO 2 => HELLO 4 => HELLO 1 => main

閲完此文,您的感想如何?
  • 有用

    3

  • 沒用

    1

  • 開心

    1

  • 憤怒

    1

  • 可憐

    1

1.如文章侵犯了您的版權,請發郵件通知本站,該文章將在24小時内刪除;
2.本站標注原創的文章,轉發時煩請注明來源;
3.交流群: 2702237 13835667

相關課文
  • mac開發接入微信公衆號接口返回報錯 cURL error 56: SSLRead() return error -9806

  • PHP的換行符是什麽

  • pecl安裝程序時報錯Array and string offset access syntax with curly braces is no longer supported

  • 由於商家傳入的H5交易參數有誤,該筆交易暫時無法完成,請聯繫商家解決

我要說說
網上賓友點評
1 樓 IP 114.254.***.129 的嘉賓 说道 : 很久前
老老实实地去swoole-&gt;task吧