0

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

1
  • php is multiprocessing, usage of pcntl_fork makes this possible! Commented Aug 16, 2011 at 14:06

3 Answers 3

2

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

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

2 Comments

Thank you Ivan for the suggestion. I am working in an environment where I do not have administrative rights to install such an extension.However I will definitely look into this for my own satisfaction and knowledge so thanks again.
@Ivan Gearman is good too for multi-processing. Multi-threading has been available in PHP since 2012. Check it out Pthreads . Also PHP App Server has been built with pthreads.
0

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.

1 Comment

not crossplatform, as it doesn't works on windows! and its about creating new processes and not threads!
0

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.

Comments

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.