views:

32

answers:

2

Hello guys,

I need to create a php script that takes lots of URL's via POST and then loads the corresponding files and dumps them in the DB. The thing is that I would like to do it asynchronous, so that if I have 1000 files to get, the script won't hang till all the files are loaded. Also, every time a file it's done loading, I need to know so that I can insert it in the DB

Any ideas are appreciated.

+1  A: 

Split the script in two parts - first to collect the URLs and the second is a shell script to be run from background to get the urls inserted in the database and fetch them.

So basically the process is as follows:

Script1:

  • Gets POST
  • Inserts into database
  • Call script 2 with
  • shell_exec to run in background

Script2:

  • Get all the urls from urls_to_download
  • Fetch the URLS (consequentially or parallel, depends on you)
  • Do stuff with them
  • Save them to database.

And you are done. The POST in script1 returns immediately and the script2 is then running. All left for you is to check status (poll from database through AJAX may be) for the URLS if you want to show some information about progress.

bisko
Sounds good, but, I'm not sure if when you call shell_exec script1 waits for script2 to finish or it runs it in the background. I'll give it a try. Thanks.
mindnoise
If you make it to run background, it runs background, check the search or the php docs. I've seen it here how to make it run background.
bisko
True, I found a way to do that. It seems that I have two options, I either use fork or exec as you suggested. I'll have a look on both.
mindnoise
And as fork is not available on shared hosts (or unless not on mine). I successfully used exec. Thanks!
mindnoise
A: 

PHP is not multithreaded and perfectly synchronous. So you may not do this using PHP alone.

But you may use another language to do this task, for example JavaScript (which is asynchronous). Try node.js. It is lightning fast and has mysql bindings ;) Use http.Client to make the requests to the sites.

nikic
PHP actually supports some what of threading :) Check the docs about PCNTL http://php.net/pcntl
bisko
I was more referring to using asynchronous functions. Isn't this multithreading, too?
nikic
Unfortunately, I can't use Javascript as the request comes from another PHP script, not from browser.
mindnoise
You could send the request to the JavaScript instead ;)
nikic