homepage Welcome to WebmasterWorld Guest from 54.224.179.98
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Running a php script in the background...
Making asynchronous calls
novasoft




msg:1290436
 3:39 pm on Apr 26, 2004 (gmt 0)

Hi,

I have quite a bit of experience in ASP and have just started on PHP.

I have a script that will take a long time to process, let me call it form2.php

Now i post from form1.php to form2.php.

On form1.php I ask the user to enter his email address and tell him that once the process is complete he will be mailed

Now what i need is that when i post from form1.php to form2.php, the browser should show a message that says "processing.. mail will be sent to your address" and let the processing work in the background. I will use ignore_user_abort for this

In the background the script must do what its supposed to do

In form2.php,I have put the message which says "mail will be sent once the process is complete" as plain html on top of the page.

Below that i have put the php code which is the long process. I have used flush(), but it doesnt show the html part till the whole script is finished.

Any idea how to accomplish this?

I dont need multithreading in the true sense.

All i need is to be a able to make a asynchronous call to a say script2.php from script1.php.

Script1.php posts to itself, calls script2.php asynchronously and shows the user a message that says "ill mail you when im done"

Nothing fancy.

Currently the user is left hanging till the process by script2.php is completed and only then the mail is sent which defeats the purpose.

Would posting to script2.php using sockets be a solution?

 

hakre




msg:1290437
 11:02 am on Apr 27, 2004 (gmt 0)

hi,

using sockets might be a solution. first of all, form2.php should not stop if the connection is stopped. that's important. you can override this php-default-behavior (when the user calls stop in the browser, the script would normally stop, too) with a php built in command (see php docs for this): set_time_limit() [de2.php.net]. this won't work in safemode! and i guess system() [de2.php.net] should be your friend instead of using sockets, because this will prevent the max execution time setting [de2.php.net] messing up your script execution. additionally, see the note in devshed at the end.

so if you call form2.php via sockets, the script will start and it won't stop, even if you cancel the socket operation. i think, the only thing you need to do is to get the headers only and then close the connection.

just give it a try. haven't done that before that way. i this will not work, give system a try.

i remember also some more infos about that topic here on webmasterworld, but can't locate the thread(s).

some more stuff:

Background tasks possible with PHP (again) [webkreator.com] via register_shutdown_function / apache_register_shutdown_function

from: http://php.net/exec [php.net]:

If you want to execute background processes from PHP that should run indefinitely, be aware of the following nasty behavior (confirmed with PHP 4.3.1 under FreeBSD, but probably under other flavors of UNIX, too):

PHP internally calls setitimer(2) to set the profiling interval timer (ITIMER_PROF) to enforce execution time limits (max_execution_time and max_input_time). These timer values are passed down to all processes executed by PHP. This means that after such a process has consumed [max_execution_time] seconds of CPU time (not wallclock time!), the system will send it a SIGPROF (signal 27). Since most processes don't use this signal and as such don't call signal(3) to catch or ignore it, the default action is executed, which is to terminate the process.

So if you see background processes that were executed from PHP dying at seemingly random times, you may be experiencing this issue. The solution is simply to call set_time_limit(0) just before executing the process.

and some more rumors about forking a background process in *nix (using the '&'?) on devshed [forums.devshed.com] (see at the end of the page).

--hakre

ruserious




msg:1290438
 5:48 pm on Apr 28, 2004 (gmt 0)

Below that i have put the php code which is the long process. I have used flush(), but it doesnt show the html part till the whole script is finished.

Are you using output buffering? Is gzip enabled? Both will prevent flush() from working correctly, IIRC.

Otherwise some Browsers also buffer the response before they start processing it. In that case it helps to send carachters to "fill up" like <!-- ....lots of dotshere.... -->

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved