Swoole Runtime HOOK Flags

Version: Swoole: 4.1.0+

Since Swoole v4.1.0, it is possible to enable coroutine on any IO libraries based on php_stream.

Libraries supported:

  • Redis
  • mysqlnd PDO, mysqli
  • SOAP
  • file_get_contents, fopen
  • stream_socket_client
  • fsockopen

Libraries not supported:

  • MySQL with libmysqlclient
  • CURL with libcurl
  • MongoDB with mongo-c-client
  • pdo_pgsql, pdo_ori, pdo_odbc, pdo_firebird

You can eanble the coroutine support with HOOK Flags.

Swoole Runtime HOOK Flags

  • SWOOLE_HOOK_ALL
  • SWOOLE_HOOK_SLEEP
  • SWOOLE_HOOK_FILE
  • SWOOLE_HOOK_CURL
  • SWOOLE_HOOK_TCP
  • SWOOLE_HOOK_UDP
  • SWOOLE_HOOK_UNIX
  • SWOOLE_HOOK_UDG
  • SWOOLE_HOOK_PROC
  • SWOOLE_HOOK_SSL
  • SWOOLE_HOOK_TLS
  • SWOOLE_HOOK_STREAM_FUNCTION
  • SWOOLE_HOOK_BLOCKING_FUNCTION

Set Coroutine Runtime flags

<?php
Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_ALL | SWOOLE_HOOK_CURL);
<?php
Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_SLEEP);

Or,

<?php
$sch = new Swoole\Coroutine\Scheduler();
$sch->set(['hook_flags' => SWOOLE_HOOK_ALL]);

Get the Swoole Coroutine Runtime flags

<?php
Swoole\Runtime::getHookFlags();

Set the Swoole Coroutine Runtime flags

<?php
Swoole\Runtime::setHookFlags(SWOOLE_HOOK_ALL);
Swoole\Runtime::setHookFlags(0);

Disable all coroutine hooks

<?php
Swoole\Runtime::enableCoroutine(FALSE);