Php中的多线程

I understand that PHP does not support multithreading but I would love to know if there is a good workaround for executing several functions in php concurrently?? I wrote some code that calculates moments of invariance. There are seven functions calculating each moment with each moment subsequently slower to fully execute than the next.

Any suggestions welcomed.

Thanks

It seems gearman is what you need. There is also a php extension

Also take a look at the pcntl_fork function (pcntl_fork)

I generally use this to spawn children from a worker. Then I use the main thread to watch the children and handle harvesting dead children and spawning new ones.

Leaving aside invoking new processes via fork, most modern operating systems (even including MSWindows) have the facility to spawn non-blocking processes from the shell, albeit that the syntax varies. So you could use the various program execution functions to invoke them.

Another approach would be to split the functionality into multiple URLs (probably restricting access to localhost) then using curl_multi_exec() to invoke them from a controlling script (note that this is likely to be less efficient than running them as seperate processes, which in turn will be less efficient than running via fork).

However any discussion of how to shard a process across mutliple threads / processes is predicated by the question of whether the process itself is shardable. Also, whether sharding will improve performance. I'll leave those questions to you.