Muhmmad Aziz
Muhmmad Aziz

Reputation: 391

Is threading in PHP efficient or not?

I'm writing a php code for a web server where it's required to do some heavy duty processes when requested before returning the results to the users. My question is: does the apache server creates a separate thread/process for each client or should I use multi-threading to separate them? The processes include calling the execution of other applications through cmd and downloading files to the server.

Upvotes: 1

Views: 95

Answers (2)

Jack
Jack

Reputation: 1606

You can't have multithreading in php with apache within a single web request. You simply can't. Usually at each request apache forks a new O.S. process. This is configurable, but typically chosen when working with php, since many methods of php standard library are not thread safe.

When I had to handle heavy computation I always choose to make the user request asynchronous, and let a third-process daemon to do the actual computation in background. In this case, after the user request, I let the client to poll the daemon (through others web-requests) to know when the computation is done.

Upvotes: 1

Kristian Lilov
Kristian Lilov

Reputation: 610

Well every request to the web server is a separate process which will try to use a free core from the CPU, and if there isn't a free one currently, it will go on a que and wait.

Upvotes: 1

Related Questions