Closed dbjpanda closed 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.
@prolic Yes it is for web application. Could you please provide a code sample or modify the above example?
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.
And this is only possible with https://github.com/amphp/http-server, not with nginx or apache?
this has nothing to do with nginx / apache. when using php-fpm or php apache module, you have once process per request.
@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.
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
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?
Right @kelunik It won't execute 'finish' untill the loop completes execution.
@kelunik, the asyncronous concept, that non blocking way, output finished outside promise and not wait the promise finished
It seems like all questions have been answered. If not, please respond with further questions, the issue can always be re-opened. :+1:
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
andFinished
as soon as possible without waiting forSend Mail
andSend SMS
.