I want close a connection early in Laravel, i found the question:
How do I close a connection early?
the answer is:
<?php
ob_end_clean();
header("Connection: close");
ignore_user_abort(true); // just to be safe
ob_start();
echo('Text the user will see');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush(); // Strange behaviour, will not work
flush(); // Unless both are called !
// Do processing here
sleep(30);
echo('Text user will never see');
?>
but echo
not work in Laravel Controller, when i change is to return
, the code behind is not executed.
[update]
My question is:
I have a time-consuming task that needs to call a python script. This task may take 30 to 180 seconds. If I use the scheduled task in Laravel or the system's Conrtab timed task, the worst may be that the Python script will only run after 1 minute. This will make the user wait an extra minute, and the user experience is not very good.
So I hope to end the connection early to let the user see the response first, and call the Python script for task processing at the same time.
Currently I used two ajax requests to solve this problem, one of which is used to call the python script.
My question is to know if the same goal can be achieved without an additional request, or if there is a better solution.