Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]处理协程请求报错 #16

Closed
sisy110 opened this issue Oct 11, 2024 · 2 comments
Closed

[BUG]处理协程请求报错 #16

sisy110 opened this issue Oct 11, 2024 · 2 comments
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@sisy110
Copy link

sisy110 commented Oct 11, 2024

问题描述

代码如下

declare(strict_types=1);

namespace app\controller;

use support\Request;
use Webman\Http\Response;
use Workbunny\WebmanCoroutine\Utils\Coroutine\Coroutine;
use Workbunny\WebmanCoroutine\Utils\WaitGroup\WaitGroup;

class CoroutineController {

    public function index(Request $request): Response {
        $timeOne=microtime(true);
        $waitGroup=new WaitGroup();

        //协程1
        $waitGroup->add();
        $coroutine1=new Coroutine(function()use($waitGroup){
            task1();
            $waitGroup->done();
        });

        //协程2
        $waitGroup->add();
        $coroutine2=new Coroutine(function()use($waitGroup){
            task2();
            $waitGroup->done();
        });

        //协程3
        $waitGroup->add();
        $coroutine3=new Coroutine(function()use($waitGroup){
            task3();
            $waitGroup->done();
        });

        $waitGroup->wait();
        $timeTwo=microtime(true);
        $totalTime=$timeTwo-$timeOne;
        echo'[x][总执行时间]'.$totalTime.PHP_EOL;
        return json(['总执行时间'=>$totalTime]);
    }


}

报错信息

PHP Warning:  [Fatal error in R14] Uncaught Error: Call to a member function getId() on null in /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/vendor/workbunny/webman-coroutine/src/Utils/Coroutine/Handlers/SwowCoroutine.php:24
Stack trace:
#0 [internal function]: Workbunny\WebmanCoroutine\Utils\Coroutine\Handlers\SwowCoroutine->Workbunny\WebmanCoroutine\Utils\Coroutine\Handlers\{closure}()
#1 /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/vendor/workbunny/webman-coroutine/src/Utils/Coroutine/Handlers/SwowCoroutine.php(22): Swow\Coroutine::run()
#2 /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/vendor/workbunny/webman-coroutine/src/Utils/Coroutine/Coroutine.php(54): Workbunny\WebmanCoroutine\Utils\Coroutine\Handlers\SwowCoroutine->__construct()
#3 /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/app/controller/coroutineController.php(20): Workbunny\WebmanCoroutine\Utils\Coroutine\Coroutine->__construct()
#4 /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/vendor/workerman/webman-framework/src/App.php(322): app\controller\CoroutineController->index()
#5 /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/vendor/workerman/webman-framework/src/App.php(169): Webman\App::Webman\{closure}()
#6 /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/vendor/workerman/workerman/Connection/TcpConnection.php(646): Webman\App->onMessage()
#7 [internal function]: Workerman\Connection\TcpConnection->baseRead()
#8 /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/vendor/workbunny/webman-coroutine/src/Events/SwowEvent.php(90): call_user_func()
#9 [internal function]: Workbunny\WebmanCoroutine\Events\SwowEvent->Workbunny\WebmanCoroutine\Events\{closure}()
#10 {main}
  thrown in /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/vendor/workbunny/webman-coroutine/src/Utils/Coroutine/Handlers/SwowCoroutine.php on line 24
@sisy110 sisy110 added the question Further information is requested label Oct 11, 2024
@chaz6chez
Copy link
Member

task方法能否提供一下?

@chaz6chez chaz6chez added the bug Something isn't working label Oct 11, 2024
@chaz6chez chaz6chez changed the title 处理协程请求报错 [BUG]处理协程请求报错 Oct 11, 2024
@chaz6chez chaz6chez self-assigned this Oct 11, 2024
@sisy110
Copy link
Author

sisy110 commented Oct 11, 2024

declare(strict_types=1);

namespace app\controller;

use support\Request;
use Webman\Http\Response;
use Workbunny\WebmanCoroutine\Utils\Coroutine\Coroutine;
use Workbunny\WebmanCoroutine\Utils\WaitGroup\WaitGroup;

class CoroutineController {

    public function index(Request $request): Response {
        $timeOne=microtime(true);
        $waitGroup = new WaitGroup();
// 协程1
        $waitGroup->add();
        $coroutine1 = new Coroutine(function () use ($waitGroup) {
            echo 1 . PHP_EOL;
            $waitGroup->done();
        });

// 协程2
        $waitGroup->add();
        $coroutine2 = new Coroutine(function () use ($waitGroup) {
            echo 2 . PHP_EOL;
            $waitGroup->done();
        });

        // 协程3
        $waitGroup->add();
        $coroutine3 = new Coroutine(function () use ($waitGroup) {
            echo 3 . PHP_EOL;
            $waitGroup->done();
        });
        $waitGroup->wait();
        echo 'done' . PHP_EOL;
        $timeTwo=microtime(true);
        $totalTime=$timeTwo-$timeOne;
        echo'[x][总执行时间]'.$totalTime.PHP_EOL;
        return json(['总执行时间'=>$totalTime]);
    }
}

代码如上 任然报错

Warning: [Fatal error in R16] Uncaught Error: Call to a member function getId() on null in /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/vendor/workbunny/webman-coroutine/src/Utils/Coroutine/Handlers/SwowCoroutine.php:24
Stack trace:
#0 [internal function]: Workbunny\WebmanCoroutine\Utils\Coroutine\Handlers\SwowCoroutine->Workbunny\WebmanCoroutine\Utils\Coroutine\Handlers\{closure}()
#1 /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/vendor/workbunny/webman-coroutine/src/Utils/Coroutine/Handlers/SwowCoroutine.php(22): Swow\Coroutine::run()
#2 /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/vendor/workbunny/webman-coroutine/src/Utils/Coroutine/Coroutine.php(54): Workbunny\WebmanCoroutine\Utils\Coroutine\Handlers\SwowCoroutine->__construct()
#3 /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/app/controller/coroutineController.php(33): Workbunny\WebmanCoroutine\Utils\Coroutine\Coroutine->__construct()
#4 /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/vendor/workerman/webman-framework/src/App.php(322): app\controller\CoroutineController->index()
#5 /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/vendor/workerman/webman-framework/src/App.php(169): Webman\App::Webman\{closure}()
#6 /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/vendor/workerman/workerman/Connection/TcpConnection.php(646): Webman\App->onMessage()
#7 [internal function]: Workerman\Connection\TcpConnection->baseRead()
#8 /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/vendor/workbunny/webman-coroutine/src/Events/SwowEvent.php(90): call_user_func()
#9 [internal function]: Workbunny\WebmanCoroutine\Events\SwowEvent->Workbunny\WebmanCoroutine\Events\{closure}()
#10 {main}
  thrown in /mnt/d/other/vm2/php_code/saiadmin/saiadmin-php/vendor/workbunny/webman-coroutine/src/Utils/Coroutine/Handlers/SwowCoroutine.php on line 24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants