3

I am trying to get multiple php scripts to run in Parallel. I am running WAMP meaning no easy access to bash or cronjobs. Hence the attempt to make this work using php only.

<?php
include("test2.php");
include("test3.php");
include("test4.php");
?>

but the test files have a wait command before outputting a line of code. When I run this, it waits for each individual php to finish it's wait time before moving on. I want them to all run at the same time (parallel)

I also tried

system('C:\wamp\bin\php\php5.4.3 test2.php &');

The goal is to make the script run these scripts in parallel, wait for them to finish running in parallel and output an echo that says finished. Am I missing something?

SuperMar1o
  • 670
  • 8
  • 23
  • 1
    Windows = Task Scheduler, Linux = bash/Cron – Daryl Gill May 02 '13 at 02:46
  • fine, be that as it may. I am trying to contain this within a single script with the ability to move it around from computer to computer without setting up tasks... – SuperMar1o May 02 '13 at 02:47
  • In that case.. If your wishing PHP to process all together, unfortunatly this wont happen. PHP works from line 1 down to the end of processing.. So essentially, your first include, will get processed.. Then output, then your second include will be processed then output, and lastly your last include will be processed then output given – Daryl Gill May 02 '13 at 02:49
  • technically incorrect using methods like this http://www.phproots.com/multithreading-in-php/ I was just hoping for someone who had a more efficient method then this one. – SuperMar1o May 02 '13 at 02:52
  • try this : http://phplens.com/phpeverywhere/?q=node/view/254 – Dinesh May 02 '13 at 02:57

3 Answers3

2

One more thing to think about, if you want this to run in a circle, just add this to the header. It will wait for the iFrames to load and then move to the next page.

<body onload=window.location='parsethem.php'> 
Elton John
  • 68
  • 6
0

Why does NO one ever mention using iframes. Genius!

<IFRAME SRC="example1.php" WIDTH=0 HEIGHT=0></IFRAME>
<IFRAME SRC="example2.php" WIDTH=0 HEIGHT=0></IFRAME>
<IFRAME SRC="example3.php" WIDTH=0 HEIGHT=0></IFRAME> 

Can run from within a PHP file and allows multiple scripts to be run at once. doh. Exactly what I needed and works like a charm

SuperMar1o
  • 670
  • 8
  • 23
  • 2
    i frames are a terrible idea,what's the browser point, theses should be called via the command line. –  May 02 '13 at 03:43
  • using iframes means these are able to be run from within a single php file, the load simultaneity and when done I can just load another php file using javascript window.onload. Yeah messy and technically the incorrect way to do it, but sometimes the results are all that matters. – SuperMar1o May 02 '13 at 03:44
  • Call it what you may, this method works PERFECTLY for me... It runs multiple php files in parallel and waits for them all to finish before moving on. It is 100% running in parallel. While you may not call it as such, at least using WAMP, it does. – SuperMar1o May 20 '13 at 00:56
  • @SuperMar1o, hello. Would you be so kind and tell me more about how you made multiple scripts to run in parallel? In my case, the browser waits with its code after the php script in ` – Bunkai.Satori Oct 30 '13 at 22:22
0

As far as I know PHP is interpreted and executes code statement by statement.

CURL or readfile() may support echoing content as it streams but only proceeds after the call is finished. The iframe solution would work; alternatively javascript for calling pages asyncronously; you could run a bash command with backticks `` but it's not portable nor good practices.

Here's a shell script option: https://serverfault.com/a/456498

Community
  • 1
  • 1
s6712
  • 496
  • 5
  • 14