开发者

PHP - Calling pcntl_fork from web app (not cli) - Why not?

I am familiar with the various methods available within php for spawn开发者_如何转开发ing new processes, forking, etc... Everything I have read urges against using pcntl_fork from within a web-accessible app. Can anyone tell me why this is not recommended?

At a fundamental level, I can see how if you are not careful, things could quickly get out of hand. But what if you are careful? In my case, I would like to pcntl_fork my parent script into a new child, run a short series of specific functions, and then close the child. Seems pretty straightforward, right? Would it still be dangerous for me to try this?

On a related note, can anyone talk about the overhead involved in doing this a different way... Calling proc_open() to launch an entirely new PHP process? Will I lose any possible speed increase by having to launch the new process?

Background: Consider a site with roughly 2,000 concurrent users running fastcgi.


Have you considered gearman for 'forking' new processes? It's also described as 'a distributed forking mechanism' so your workers do not need to be on the same machine.

Synchronous and asynchronous calls are also available.

You will find it here: http://gearman.org/ and it might be a candidate solution to the problem.


I would like to propose another possibility... Tell me what you think about this.

What if I created a pool of web servers whose sole job was to respond to job requests from the master application server? I would have something like this:

Master Application Server (Apache, PHP - FastCGI)

  • Application Worker Server (Apache, PHP - FastCGI)
  • Application Worker Server (Apache, PHP - FastCGI)
  • Application Worker Server (Apache, PHP - FastCGI)
  • Application Worker Server (Apache, PHP - FastCGI)

Instead of spawning new PHP processes on my master application server, I would send out job requests to my "workers" using asynchronous sockets. The workers would then run these jobs in realtime and send the result back to the main application server.

Has anyone tried this? Do you foresee any problems? It seems to me that this might work wonderfully.


The problem is not that the app is web-accessible.

The problem is that the web server (or here the FastCGI module) may not handle forks very well. Just try yourself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜