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 ??

2

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

how can send data to child made by fork ?
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.
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...

3 Comments

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.
@George, utterly agree... this is just a simple easy fix.
@Geoffrey please accept my apologies. Should NEVER, EVER, write anything down while talking on the phone at the same time. Sorry! ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.