dongli2000 2016-12-02 20:29
浏览 56

使用React / Ratchet,pthreads无法正常工作

I'm trying to add pecl-pthreads multithreading support to Ratchet WebSocket app.

This is my application:

<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface
{
    protected $clients;
    public function __construct()
    {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn)
    {
        $this->clients->attach($conn);
        echo "New connection! ({$conn->resourceId})
";
    }

    public function onMessage(ConnectionInterface $from, $msg)
    {
        $dataIn = json_decode($msg);
        switch ($dataIn->type) {
            case 'test1':
                echo '+t1 block start' . date(' H:i:s') . "
";
                $test1 = new Test1();
                $test1->start();
                echo '+t1 block end' . date(' H:i:s') . "
";
                break;
            case 'test2':
                echo '-t2 block start' . date(' H:i:s') . "
";
                $test2 = new Test2();
                $test2->start();
                echo '-t2 block end' . date(' H:i:s') . "
";
                break;
        }
    }

    public function onClose(ConnectionInterface $conn)
    {
        $this->clients->detach($conn);
        echo "Connection {$conn->resourceId} has disconnected
";
    }

    public function onError(ConnectionInterface $conn, \Exception $e)
    {
        echo "An error has occurred: {$e->getMessage()}
";
        $conn->close();
    }
}

This is Test1.php content:

<?php
namespace MyApp;
class Test1 extends \Thread
{
    public function run()
    {
        echo '#Test1 thread start' . date(' H:i:s') . "
";
        for ($i = 0; $i < 2; $i++) {
            echo 1 . date(' H:i:s') . "
";
            sleep(10);
        }
        echo '#Test1 thread end' . date(' H:i:s') . "
";
    }
}

This is Test2.php content:

<?php
namespace MyApp;
class Test2 extends \Thread
{
    public function run()
    {
        echo '#just Test2 run' . date(' H:i:s') . "
";
    }
}

And this is JavaScript code:

var Websocket = new function () {
    var ws
    this.init = function () {
        ws = new WebSocket(wsURL + '/test')
        ws.onclose = function () {
            setTimeout('Websocket.init()', 1000)
        }
    }
    this.test1 = function () {
        var token = {
            type: 'test1'
        }
        ws.send(JSON.stringify(token))
    }
    this.test2 = function () {
        var token = {
            type: 'test2'
        }
        ws.send(JSON.stringify(token))
    }
}
$(document).ready(function () {
    Websocket.init()
})

var startTest = function () {
    Websocket.test2()
    Websocket.test2()
    Websocket.test1()
    Websocket.test2()
    Websocket.test2()
}

The problem is that Test1 thread is bloking onMessage method of MyApp.

When I connect to my app with browser and run JavaScript startTest() function I get this CLI application output:

-t2 block start 20:08:48
#just Test2 run 20:08:48
-t2 block end 20:08:48
-t2 block start 20:08:48
#just Test2 run 20:08:48
-t2 block end 20:08:48
+t1 block start 20:08:48
#Test1 thread start 20:08:48
+t1 block end 20:08:48
1 20:08:48                                                                                                                                                                                                                                     
1 20:08:58                                                                                                                                                                                                                                     
#Test1 thread end 20:09:08                                                                                                                                                                                                                     
-t2 block start 20:09:08                                                                                                                                                                                                                       
#just Test2 run 20:09:08                                                                                                                                                                                                                       
-t2 block end 20:09:08                                                                                                                                                                                                                         
-t2 block start 20:09:08                                                                                                                                                                                                                       
#just Test2 run 20:09:08                                                                                                                                                                                                                       
-t2 block end 20:09:08

As you can see I send instant 5 messages from the browser to my application. But after test1 message there is a pause in execution of onMessage method until the end of execution of Test1 thread. Is it some kind of pthreads/ratchet bug or am I doing something wrong?

  • 写回答

1条回答 默认 最新

  • duanliaozhi2915 2019-04-18 09:22
    关注

    You send messages at one connection which means you have only single one thread. If you would like to check if multithreading is working - you should try to run that JS in next browser window / tab.

    Cuz while test1 is blocking thread, else messages (2 messages test2 left) are waiting in queue. However if someone else will connect same time to the server he will not be a blocked - as he will gain separate thread. That is the power of multithreading in this case.

    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教