-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[question] Can we use Timer::add(0, $func, $vars, false) to run tasks async #937
Comments
I'm not sure if all the drivers (event, Uv, revolt, swoole, etc.) support passing 0 as the timer value. If they do, Workerman should indeed support it as well. |
I think that name is |
With Now I'm testing with Adapterman and Symfony function run(): string
{
global $kernel;
ob_start();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
Timer::add(0.001, function () use ($kernel, $request, $response) {
$kernel->terminate($request, $response);
}, persistent: false);
//$kernel->terminate($request, $response);
return ob_get_clean();
} |
Example with Adapterman and Symfony. Using Timer to defer Timer::add(0.001, function () use ($kernel, $request, $response) {
$kernel->terminate($request, $response);
}, persistent: false);
//$kernel->terminate($request, $response); Without defer, run inmediatelly:With defer, run after enter in the event loop:The data to create the graph, it's sent in the kernel.terminate. |
To show the requests with time used with Adapterman (or Workerman) live in the terminal, it's as simple as: $http_worker->onMessage = static function ($connection, $request) {
$start = microtime(true);
$connection->send(run());
Worker::safeEcho($_SERVER['REQUEST_METHOD'] ."\t". $_SERVER['REQUEST_URI'] ."\t". round((microtime(1) - $start) * 1000, 4) . ' ms' . PHP_EOL);
}; |
Hi,
I have seen that in other frameworks, in reality is the base of async.
Timer::add(0, $func, $vars, false);
Theoretically this task will be included to schedule in the event loop.
If so, perhaps it will be better to add a
Timer::once(0, $func, $vars);
.Example:
Timer::add(0, $send_mail, $vars, false);
What do you think @walkor ??
The text was updated successfully, but these errors were encountered: