amphp / parallel-functions

Simplified parallel processing for PHP based on Amp.
https://amphp.org/parallel-functions
MIT License
268 stars 18 forks source link

Is it possible to end the code execution without waiting for Promises? #21

Closed dbjpanda closed 4 years ago

dbjpanda commented 4 years ago

How can I run Prmoise\wait() in the background and execute the next line without waiting for the promises to be finished? I meant I just wanted to complete the code execution without caring for anything else. Let the child processes to be run even after the parent dies.

Say in below example I just want to print Started and Finished as soon as possible without waiting for Send Mail and Send SMS.

<?php

require "vendor/autoload.php";

use Amp\Promise;
use Amp\ParallelFunctions;

echo 'started</br>';

$promises[1] = ParallelFunctions\parallel(function (){
    // Send Email
})();

$promises[2] = ParallelFunctions\parallel(function (){
    // Send SMS
})();

Promise\wait(Promise\all($promises));

echo 'finished';
prolic commented 4 years ago

Is this meant for cli processes? if so, the answer is no. when running in a web application using http://github.com/amphp/http-server, then yes that's possible, you can f.e. use Loop::defer and do it in the background.

dbjpanda commented 4 years ago

@prolic Yes it is for web application. Could you please provide a code sample or modify the above example?

prolic commented 4 years ago

Try this:

Loop::defer(function (): Generator {
    $promises[1] = ParallelFunctions\parallel(function (){
    // Send Email
    })();

    $promises[2] = ParallelFunctions\parallel(function (){
    // Send SMS
    })();

    yield Promise\all($promises);
});

This way you can defer the execution of a callback.

dbjpanda commented 4 years ago

And this is only possible with https://github.com/amphp/http-server, not with nginx or apache?

prolic commented 4 years ago

this has nothing to do with nginx / apache. when using php-fpm or php apache module, you have once process per request.

kelunik commented 4 years ago

@dbjpanda No it's currently not possible to disown processes using amphp/parallel or amphp/process. I'd recommend looking into job queues such as RabbitMQ. If nohup works, you could use that, but then you don't need Amp at all.

derit commented 4 years ago

use this code

use Amp\Promise;
use Amp\ParallelFunctions;

Amp\Loop::run(function () {
Amp\Loop::defer(function (): Generator {
    $promises[1] = ParallelFunctions\parallel(function (){
    // Send Email
    echo "Send Email\n";
    sleep(5);
    echo "Finish Email\n";
    })();

    $promises[2] = ParallelFunctions\parallel(function (){
    // Send SMS
    echo "Send SMS\n";
    sleep(5);
    echo "Finish sms\n";
    })();

    yield Promise\all($promises);
});
echo  "finished\n";
});

execute "finished" without wait promise

kelunik commented 4 years ago

I'll directly output finished, but the loop will continue to run until all watchers are either disabled or unreferenced.

I guess it won't reach below Loop::run() before the promises resolved?

dbjpanda commented 4 years ago

Right @kelunik It won't execute 'finish' untill the loop completes execution.

derit commented 4 years ago

@kelunik, the asyncronous concept, that non blocking way, output finished outside promise and not wait the promise finished

kelunik commented 4 years ago

It seems like all questions have been answered. If not, please respond with further questions, the issue can always be re-opened. :+1: