0

explain by example : f.php :

d$=$_Get['ad'];
 print d$;

index.php :

 for $i=0 to 200000
     // run f.php?ad=i$

run f.php but dont wait to finish f.php how can do that?

  1. i find php asynchronus but i dont now this is working realy or existing other solution or is this best solution?!!

  2. when use exec and how ??

Community
  • 1
  • 1
naser
  • 4,373
  • 4
  • 18
  • 13
  • exec is used to run external code such as getting system stats from your program. You really want to look into forking. – Geoffrey Wagner Jun 10 '11 at 18:59
  • 1
    Related questions: [php execute a background process](http://stackoverflow.com/questions/45953/php-execute-a-background-process), [How can I run a PHP script in the background after a form is submitted?](http://stackoverflow.com/questions/4626860/how-can-i-run-a-php-script-in-the-background-after-a-form-is-submitted) – netcoder Jun 10 '11 at 19:04

2 Answers2

5

The term your looking for is called forking. Here is a link to all the PHP docs you will need to fork your code into async procs.

http://php.net/manual/en/function.pcntl-fork.php

Geoffrey Wagner
  • 818
  • 1
  • 5
  • 11
  • how can send data to child made by fork ? – naser Jun 10 '11 at 19:17
  • define it in a var such as $child_data = "some data"; and then fork your child process (child processes have access to all the variables in the parent process). The only things that cant be forked are resource connections such as mysql, those need to be recreated inside of the new fork. – Geoffrey Wagner Jun 10 '11 at 19:19
1

If you're running linux and have access to php CLI you can have something like this:

<?php
// this will launch worker to run something... 
shell_exec('php worker.php >/dev/null &');

// the rest of the flow goes here
...
?>

worker.php could be writting stuff to a database, sending emails, whatever...

Frankie
  • 24,627
  • 10
  • 79
  • 121
  • shell_exec is still blocking as it returns the output. Note the & at the end to foce things (in linux/unix) to run in the background. If you dont add the & then this solution wont work, also not ideal for firing off background work as this will become very cumbersome in the longrun. – Geoffrey Wagner Jun 10 '11 at 19:07
  • 1
    @George, utterly agree... this is just a simple easy fix. – Frankie Jun 10 '11 at 19:14
  • @Geoffrey please accept my apologies. Should NEVER, EVER, write anything down while talking on the phone at the same time. Sorry! ;) – Frankie Jun 10 '11 at 19:20