![]() |
Asynchronous Script call in CI - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Asynchronous Script call in CI (/showthread.php?tid=22864) |
Asynchronous Script call in CI - El Forum - 09-22-2009 [eluser]Unknown[/eluser] Hi all -- newbie here. I've an application that takes user input, saves to a database, compiles an email and sends a redirect page back to the user. The user, while this is going on, is held hostage for 5 seconds or so while the script completes. Is there anyway to send back the user page and continue with server processing? How could I do that instead of having them wait for the <return> Thanks! -Dev Asynchronous Script call in CI - El Forum - 09-22-2009 [eluser]gon[/eluser] In this case the best thing you can do is storing all the info for sending the email and redirecting the user to the next screen. Then a background process will send every pending mail. You can store the emails info in files, or better, have a database table that stores subjects, bodies and addresses. For sending emails, you can use a php script as a daemon, or have an script executed every minute via cron. This script will check if there is any pending mail and send it. If you use cron, take care of checking if script is still executing (this happens when there is a lot of emails and an execution lasts more than one minute). For this it's nice having a controller for executing codeigniter code from the command line. Check the wiki for this. hope this helps. Asynchronous Script call in CI - El Forum - 09-22-2009 [eluser]BrianDHall[/eluser] If you want to run a PHP process in the background this is annoyingly non-trivial on windows - but is possible, and on unix is easy. This is one of those things that you think should be easy, because even Perl has this thanks to Fork()...but PHP doesn't. Go figure. Anyway, basically what you do is either as gon suggests above with a cronjob, or you need your script to execute another script in the background using exec() (linux only) or popen() (windows or unix). I got this code from the exec() listing in the php manual [code] <?php function execInBackground($path, $exe, $args = "") { global $conf; if (file_exists($path . $exe)) { chdir($path); if (substr(php_uname(), 0, 7) == "Windows"){ pclose(popen("start \"bla\" \"" . $exe . "\" " . escapeshellarg($args), "r")); } else { exec("./" . $exe . " " . escapeshellarg($args) . " > /dev/null &"); } } } ?> [/close] Yeah, so having a 'fork' helper would be damn nice, wouldn't it? Anyway, this is obviously not a beginner solution. The easier way is to give them an 'interstitial' page that gives them the 'please wait thing', with a few second delay javascript redirect to the real page. ...yeah, I would strongly recommend that way. |