Welcome Guest, Not a member yet? Register   Sign In
Asynchronous Script call in CI
#1

[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
#2

[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.
#3

[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]
&lt;?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 &");
}
}
}
?&gt; [/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.




Theme © iAndrew 2016 - Forum software by © MyBB