Yii2 使用Pthreads多线程扩展遇到无法引用Yii和其他类的问题

bug问题 · thinfell · 于 6年前 发布 · 2610 次阅读
namespace frontend\controllers;

use yii\web\Controller;
use thinfellpthreads\PthreadsJiangSuYinHangCode;

class JiangSuCodeController extends Controller
{
    public $ip2 = '122.114.228.54:11080';
    public $iptype2 = 0;

    public function actionIndex()
    {
        for ($xx = 1; $xx <= 3; $xx++) {
            $requests[] = new PthreadsJiangSuYinHangCode($this->iptype2, $this->ip2);
        }

        foreach($requests as $thread) {
            $thread->start();
        }
        foreach($requests as $index => $thread) {
            $thread->join();
        }
        echo '处理完成';
    }
}

在这个文件里面 use不了其他的方法 我在看站长大人 在Yii2上面的帖子 能运行 我这个就不行了

共收到 6 条回复
thinfell#16年前 0 个赞

补充一下 那个线程文件里面 用原生脱离YII的方法是完全正常的运行 唯独不能用Yii和vendor里面的方法

thinfell#26年前 0 个赞

__construct 这里 new 好了 下面 $this来用 还是依然报错Fatal error: Call to a member function get() on null in vendor\yiisoft\yii2\BaseYii.php on line 344

Fecmall#36年前 0 个赞

这个很久之前用到过的

好像是这个样子的,在php 多线程里面,Yii2的一些方法是不能用的,这个也是为了线程安全,相互不冲突。

具体问题您可以去github哪里提问。多看里面的介绍

thinfell#46年前 0 个赞

@Fecshop #3楼 好的 老大 的确是 一些方法不可以

Fecmall#56年前 0 个赞

@thinfell #4楼 您可以看一下,我之前的例子:https://blog.csdn.net/terry_water/article/details/50273847

Fecmall#66年前 0 个赞

@thinfell #2楼

类似这个:

public function __construct($order)    
    {    
        # 下面程序使用的对象,必须在构造方法中实例化,不可以在调用的对象中new对象,会报错  
        $this->order = $order;    
        $this->Shippingpthreads = new Shippingpthreads;   
          
        $this->bilishi           = new bilishi;   
        $this->dhl               = new DHL;   
        $this->eyoubao           = new Eyoubao;   
        $this->SahprShip         = new SahprShip;   
        $this->sf                = new Sf;   
        $this->csfau             = new CSFAU;   
        $this->XML2Array         = new XML2Array;   
        $this->Array2XML         = new Array2XML;   
    }    

你需要把你要用到的东西,引入到对象变量赋值,而且,你引入的对象,是独立的,引入的对象中好像不能在引入其他的对象,譬如你上面说的Yii::

另外静态方法是不行的,因为php的多线程,每个线程都是独立的,以免干扰,保证线程安全,我研究的不深入,当时为了订单获取物流号,采用了多线程并发获取,上面发的链接就是实现的思路,你可以参考下。

只能帮这么多,我没有太深入研究这个php扩展,满足了我的需要后,就没有再看。

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics