自定义进程内有关协程的问题

willvar

问题描述

自定义进程内获取协程状态、拉取当前协程,即调用 Coroutine::isCoroutine()Coroutine::getCurrent() 的结果理论上(如有遗漏请指出)应该是取决于进程所使用的驱动。

程序代码或配置

<?php
namespace app\process;
use Workerman\Coroutine;

class Test
{
  public function onWorkerStart($worker)
  {
    var_dump(Coroutine::isCoroutine());
    var_dump(Coroutine::getCurrent());
  }
}

config/process.php 进程定义如下:

<?php
...

return [
  'webman' => [
       ...
    ]
  ],
  'test' => [
    'handler' => app\process\Test::class
  ]
];

config/server.php 是没有改过的:

<?php
/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */

return [
    'event_loop' => '',
    'stop_timeout' => 2,
    'pid_file' => runtime_path() . '/webman.pid',
    'status_file' => runtime_path() . '/webman.status',
    'stdout_file' => runtime_path() . '/logs/stdout.log',
    'log_file' => runtime_path() . '/logs/workerman.log',
    'max_package_size' => 10 * 1024 * 1024
];

重现问题的步骤

  • 新建项目,如 composer create-project workerman/webman:~2.0
  • 新建自定义进程,如上

得到的结果:

[len@r1 webman]$ php start.php restart
Workerman[start.php] restart
Workerman[start.php] is stopping ...
Workerman[start.php] stop success
-------------------------------------------- WORKERMAN ---------------------------------------------
Workerman/5.1.3         PHP/8.4.11 (JIT on)           Linux/6.15.9-arch1-1
--------------------------------------------- WORKERS ----------------------------------------------
event-loop  proto       user        worker      listen                 count       state            
event       tcp         len         webman      http://0.0.0.0:8787    64           [OK]            
event       tcp         len         test        none                   1            [OK]            
----------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
bool(true)
object(Workerman\Coroutine\Coroutine\Fiber)#70 (2) {
  ["fiber":"Workerman\Coroutine\Coroutine\Fiber":private]=>
  object(Fiber)#60 (0) {
  }
  ["id":"Workerman\Coroutine\Coroutine\Fiber":private]=>
  int(1)
}

和我预想的不太一致,这意思是 event 也支持协程了吗?找了一圈源码也没发现啥问题,所以来请教大家。

操作系统环境及workerman/webman等具体版本

[len@r1 ~]$ uname -a
Linux r1 6.15.9-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 02 Aug 2025 01:20:06 +0000 x86_64 GNU/Linux
[len@r1 ~]$ php -m
[PHP Modules]
apcu
bcmath
Core
ctype
curl
date
dom
event
fileinfo
filter
gd
hash
iconv
igbinary
intl
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
Phar
posix
random
readline
redis
Reflection
session
simdjson
SimpleXML
sockets
sodium
SPL
standard
tokenizer
uuid
xml
xmlreader
xmlwriter
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache

[len@r1 ~]$ php -v
PHP 8.4.11 (cli) (built: Jul 31 2025 15:55:26) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.11, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.11, Copyright (c), by Zend Technologies

composer.json

{
  ...
  "require": {
    "php": ">=8.1",
    "workerman/webman-framework": "^2.1",
    "monolog/monolog": "^2.0"
  },
  "autoload": {
    "psr-4": {
      "": "./",
      "app\\": "./app",
      "App\\": "./app",
      "app\\View\\Components\\": "./app/view/components"
    }
  },
  "scripts": {
    "post-package-install": [
      "support\\Plugin::install"
    ],
    "post-package-update": [
      "support\\Plugin::install"
    ],
    "pre-package-uninstall": [
      "support\\Plugin::uninstall"
    ]
  },
  "minimum-stability": "dev",
  "prefer-stable": true
}
310 1 1
1个回答

为了兼容,没开启swow和swoole时,onWorkerStart是在一个PHP原生Fiber环境下运行的,属于协程环境。

  • willvar 7天前

    那在这种情况下有办法让自定义进程不在协程环境运行吗

  • walkor 7天前

    对你有什么影响么

  • willvar 6天前

    这个进程里判断了下协程环境走了两套流程,然后发现全都在按协程内走😂

  • walkor 6天前

    你可以写一个类继承Worker,然后重新onWorkerStart实现

🔝