10
When I asked the question What is the solution for asynchronous PHP processes?, basically what I had in mind was this question:
"There is some way to complete a request from a client, but leave a process running in the same script that serves this client, such as a record in the database followed by an email submission?
I ended up realizing that thinking about the word "asynchronous" might not be the most correct approach, since I don’t want parallel process.
When a request is sent to a PHP script, all processing is done there to, at the end of the operations, the response is sent to the client. And the client (browser), in turn, is waiting for the end of the execution of that process.
The question I asked earlier about asynchronous processes already has several answers on how to "dribble" this small PHP problem.
But what I’d like to know is: Is there any way to, when making a request to a PHP script, send a response to the client, but that same script keeps running on the server side, until a long operation is completed?
For example:
gravar_dados_no_banco();
// Envia a resposta pro navegador
// Porém ele não precisa mais esperar o término desse script
echo json_encode(['status' => true]);
// Depois da resposta acima
// Faço algumas operações demoradas
// Que não serão "esperadas" pelo navegador
sleep(3);
mandar_email_pro_admin();
atualizar_todos_webservices();
I think what you want to do is not possible with PHP, using ajax does not help you??
– Fernando VR
@Fernandovr is possible yes, I asked the question more in the informative sense. If no one answer, I will answer, so the staff learn this trick :)
– Wallace Maxters
Ahh ok, because the easiest way I imagined to solve what you need would be in ajax requests. You send a command to record the data and return the json response, and then send another one to send the Email. I have long thought about this possibility of q vc wants to do in PHP, but never found a solution for this, since php blocks the return until the end of the execution of all script.
– Fernando VR
@Fernandovr I’ll give you a hint: https://stackoverflow.com/questions/15273570/continue-processing-php-after-sending-http-response
– Wallace Maxters
I remember answering that already. I’ll see if I can find.
– Woss
https://answall.com/a/217559/5878
– Woss
Very cool this @Wallacemaxters method, and Anderson’s response, tbm is very good. Using
ignore_user_abort(true);
andset_time_limit(0);
I even closed the browser after the return of the screen, and even then it continued running the script until the end. When I tried it in the past, I wanted it to continue displaying results on the screen without stopping the script. But this tbm method is very good.– Fernando VR
@Fernandovr. Here where I work, there is a script that I need to register that a request has been canceled, after it is canceled (bank registration), I reply to the customer that he has successfully canceled, but after that reply, I need to at the same time send an e-mail to the owner of the request and make a request on the webservice. It saved my life!
– Wallace Maxters
Ahh understood @Wallacemaxters, I’ve done something similar, but I always expected the script to finish to work everything on screen. But really this way is great and ends up gaining enough speed in response. I really liked, and now q also learned how to do, I will use a lot. Hug friend, have a great day.
– Fernando VR
@Fernandovr I’m testing here and mine is waiting for the end. But that’s not the intention. I’ll keep testing here, he has to finish the answer, but continue the script.
– Wallace Maxters