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

[question] Can we use Timer::add(0, $func, $vars, false) to run tasks async #937

Open
joanhey opened this issue Jul 10, 2023 · 5 comments
Open

Comments

@joanhey
Copy link
Contributor

joanhey commented Jul 10, 2023

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 ??

@walkor
Copy link
Owner

walkor commented Jul 11, 2023

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.

@xpader
Copy link

xpader commented Jul 11, 2023

I think that name is defer.

@joanhey
Copy link
Contributor Author

joanhey commented Aug 14, 2023

With 0 or 0.0, return Exception: bad time_interval (using select event).

Now I'm testing with Adapterman and Symfony $kernel->terminate, and for now seems that it's working.

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();
}

@joanhey
Copy link
Contributor Author

joanhey commented Sep 11, 2023

Example with Adapterman and Symfony.

Using Timer to defer $kernel->terminate($request, $response);

Timer::add(0.001, function () use ($kernel, $request, $response) {
        $kernel->terminate($request, $response);
    }, persistent: false);


//$kernel->terminate($request, $response);

Without defer, run inmediatelly:

image
image

With defer, run after enter in the event loop:

image

image

The data to create the graph, it's sent in the kernel.terminate.

@joanhey
Copy link
Contributor Author

joanhey commented Sep 11, 2023

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);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants