Thursday, July 8, 2010

True multithreading in PHP! Spawn new threads in webpage

Ok, so I searched around for this and I was disappointed, I could not find true multithreading in PHP. Well here is my solution. You know how you can call php from the command line in linux/unix? Well you can do a system call to a php function, hide the output and break off of it and come back to the website and your php script will continue executing in the background.

Here is an example:

So from the command line I can do:


well there is no reason you can't call the a system call from your website, so I am doing something like this:

$exec_string="php -r 'PUT_YOUR_PHP_CODE_HERE_WITHOUT_ESCAPES' > /dev/null 2>&1 &";

The command: 2>&1 & at the end is what I am using the escape the string rather than waiting for it to finish.

$exec_string="php -r 'mail(\"".$_POST['user']."\",\"".$string1."\",\"".$bericht."\",\"".$string2."\");' > /dev/null 2>&1 &";

1 comment:

danstefan said...

I'm bit disappointed. Looking at the title I expected something better. You have no idea what you are writing about. You do not create a new thread, it is a new process.