Jump to content

Posting a variable to a script without waiting for an answer


iamkoby

Recommended Posts

Hi !

 

I'm looking for hours for a way to call a script with a variable (it can be either GET or POST) but without waiting for the script to reply.

 

This script takes at least 7 seconds to respond so if i'm using file_get_contents I'm left with a suspend of 7+ seconds in my script.

 

I have NO interest in the response of that script since it returns nothing so how can I just call it, leaving it to work and continue in my own script ?

 

Your help is greatly appreciated. Thank you!

From another php file.

 

This is the flow:

 

User submits a form on the client side.

The php file (let's call it PHP1) receives the form and calls another script (PHP2). PHP2 builds a customized page for the submitted information and saves it in the database. This process takes 7+ seconds and returns nothing to PHP1.

PHP1 wait for PHP2 to finish (although PHP2 doesn't return anything) and that delays the user response too much.

I need PHP1 to get the form, send it (only two variables) to PHP2 and respond to the user: "Your details sent for compilation. You'll get a mail with a link when your page is ready.".

i think i would consider using AJAX for that. look up xmlhttprequest() in google. there are tons of tutorials laying around for this.

 

otherwise, i think that the windows version of php has an extension for working with multiple threads, but i know nothing about using it, and it's probably a much more difficult solution than the ajax.

right. sorry. here's what i found:

if you're web server is running on a *nix system (only *nix), php has a function called pcntl_fork() (look it up in the maual) for forking php processes to the background. it looks like this functionality is not compiled into php by default and you have to use a --enable-pcntl flag at compile.

 

what the function basically does, is copy the state of your current script to a new child process, and the child continues running from the line immediately after the function call. to make the child process run 1 thing and the parent another, you can do something like:

 

$pnID = pcntl_fork();

if ($pnID){ //since child only starts running from here, it wont have $pID initialized
   //parent code
}
else{
   //child code
}

 

i found a few tutorials that go into more detail:

http://www.electrictoolbox.com/article/php/process-forking/

http://immike.net/blog/2007/04/08/fork-php-and-speed-up-your-scripts/

http://www.welldonesoft.com/technology/articles/php/forking/

 

they're all more or less the same.

 

* note: one of the above tutorials recommends not to use this function for web server scripts, (maybe for security reasons?).

 

hope this is what you were looking for.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.