0

I have a PHP script that does a lot of checks and then downloads a file to the server and outputs the original.
To download the file I use

system("/usr/local/bin/wget -O ...");

So php waits for the download to finish and then it outputs the file.
My question is if its possible to send a "ping" somewhere (other php file?) for the download to start and then without waiting for the result just continue with the execution.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jim
  • 684
  • 2
  • 8
  • 20

3 Answers3

2

Use curl_multi_* functions to handle multiple-downloads in parallel. PHP lacks any kind of threading support and process forking is merely a hack.

Sim
  • 2,627
  • 1
  • 20
  • 21
  • I dont have multiple downloads, the file is only one. I just want to continue executing while the file is downloading. – Jim Jan 30 '12 at 13:44
1

Use pcntl_fork() to fork your current process into a child process. Do the file download in the child process while your parent process can continue executing its task.

$pid = pcntl_fork(); 

if ( $pid == -1 ) 
{        
    // Fork failed            
    exit(1); 
} 
else if ( $pid ) 
{ 
    // The parent process
   //continue what you want to do here
} 
else 
{ 
    // the child process 
    // do the system call here
}

After doing some work, if you now need to parent process to wait until the child process finishes, you can pcntl_waitpid($pid).

Read here for more documentation on pcntl methods.

Ayush
  • 41,754
  • 51
  • 164
  • 239
  • pcntrl functions are not recommended for use in web environments and only work on *nix systems - http://uk.php.net/manual/en/intro.pcntl.php – Adam Hopkinson Jan 30 '12 at 10:07
  • Since he tagged the question linux, I think we can safely assume he's running it in a linux box. As for your other point, you are correct. Pcnlt functions only run from CLI. – Ayush Jan 30 '12 at 10:09
  • Still "not recommended for use in web environments" - as in, this will Crash And Burn at the worst possible moment. – Piskvor left the building Jan 30 '12 at 10:13
  • @Piskvor: It won't 'crash and burn'. If run from a cgi environment, it will gracefully throw an error since pcntl function does not exist in the CGI version of PHP – Ayush Jan 30 '12 at 10:27
  • @xbonez: Hmm, that would explain it - I've seen this on a web server, but it must have been hacked into the source by the locals, then. Explains the picturesque crashes. – Piskvor left the building Jan 30 '12 at 10:39
0

A general design patters would be to apply a Command Design Pattern: http://en.wikipedia.org/wiki/Command_pattern

You can implement this in your local web environment or you could install some kind of queue server like: http://www.rabbitmq.com/ etc. etc.

It depends on your systems, access to them and your knowledge to see which solution would fit best That's a more complex question too far offtopic here.

In general the suggested pattern will allow you to fix your question without having lots of issues since it is a well-known and widely used approach to handle lots of tasks which take too long for doing directly within the request.

Luc Franken
  • 2,994
  • 1
  • 17
  • 14