I was trying to setup a multi-threaded socket application, but whenever I ran it I got an error because pcntl_fork()
was disabled by default. Is this because it is dangerous or unstable? Should I look for some other way to multithread, or is it just disabled because it is not often used?

- 741
- 7
- 17
-
Use [pthreads](http://pthreads.org/) if you can get it installed – Baba May 10 '13 at 12:00
2 Answers
pcntl_fork()
is not for multithreading, it only... well, forks the current proccess. Be sure to check the documentation for more information on the function.
The best reason I can think of it's disable by default, it's because PHP was never meant for parallel computing, it's merely a scripting language (a very powerful one at that). As Martin suggestted on his answer in a similar question, you're better off running a CRON or another program.
-
The options provided by my web hosters include only php and cgi scripts (supposedly I can use python with those). Would it be better to rewrite in python? I don't know python and I don't know if it has good multi-threading support or if it handles sockets well. Would you suggest that I stick with php, or that I move on to python? – CoderOfHonor May 07 '13 at 19:48
-
I suggest doing it with what you're more comfortable with. Although in this case, to do what you're wanting (multi-threading) python of obviously more recommendable, but since I don't know what results you expect with said multi-threading, you could do something similar with PHP. – fcm May 07 '13 at 19:53
It simply is a function that should not be used in the average shared-hosting apache environment. Forking there would possibly clone a rather big process and besides that the function can be abused to bring down a poorly configured server (fork bomb).
Using it e.g. in a commandline based PHP script is perfectly fine.

- 310,957
- 84
- 592
- 636