Skip to content

run in coroutine

Inhere edited this page Jan 15, 2019 · 2 revisions

协程运行命令

协程运行需要 swoole 4+ 支持

通过设置属性 protected static $coroutine 可以启用swoole协程来运行命令

<?php
/**
 * Created by PhpStorm.
 * User: inhere
 * Date: 2018-01-26
 * Time: 17:47
 */

namespace Inhere\Console\Examples\Command;

use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Console\Util\Helper;

/**
 * Class CorCommand
 * @package Inhere\Console\Examples\Command
 */
class CorCommand extends Command
{
    protected static $name = 'cor';
    protected static $description = 'a coroutine test command';
	// 启用协程运行
    protected static $coroutine = true;

    /**
     * @return array
     */
    public static function aliases(): array
    {
        return ['coro'];
    }

    /**
     * do execute
     * @param  Input $input
     * @param  Output $output
     */
    protected function execute($input, $output)
    {
        $output->aList([
            'support coroutine?' => Helper::isSupportCoroutine() ? 'Y' : 'N',
            'open coroutine running?' => self::isCoroutine() ? 'Y' : 'N',
            'running in coroutine?' => Helper::inCoroutine() ? 'Y' : 'N',
        ], 'some information');
    }
}

执行时,会自动检查是否支持协程,不支持的环境将会自动退回普通方式执行。