3

Is there anyway to work with threads in PHP via Apache (using a browser) on Linux/Windows?

3
  • Have you tried googling for, say, 'PHP thread'? Commented Nov 23, 2014 at 14:28
  • Yes, sure. But results were not clear. I am searching for PHP threads using Apache (via Browser). Google returns Threading using PHP from Command Line. Commented Nov 23, 2014 at 14:28
  • @Richard: your question is too broad, unclear ("via browser" - server side threads via browser?), does not show any code or sign of serious research. Pardon me, there are quite some reasons to downvote and flag your questions. Commented Nov 23, 2014 at 15:10

3 Answers 3

5

The mere fact that it is possible to do something, says nothing whatever about whether it is appropriate.

The facts are, that the threading model used by pthreads+PHP is 1:1, that is to say one user thread to one kernel thread.

To deploy this model at the frontend of a web application inside of apache doesn't really make sense; if a front end controller instructs the hardware to create even a small number of threads, for example 8, and 100 clients request the controller at the same time, you will be asking your hardware to execute 800 threads.

pthreads can be deployed inside of apache, but it shouldn't be. What you should do is attempt to isolate those parts of your application that require what threading provides and communicate with the isolated multi-threading subsystem via some sane form of RPC.

I wrote pthreads, please listen.

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

Comments

1

Highly discouraged.

The pcntl_fork function, if allowed at all in your setup, will fork the Apache worker itself, rather than the script, and most likely you won't be able to claim the child process after it's finished. This leads to many zombie Apache processes.

I recommend using a background worker pool, properly running as a daemon/service or at least properly detached from a launching console (using screen for example), and your synchronous PHP/Apache script would push job requests to this pool, using a socket.

Does it help?

[Edit] I would have offered the above as a commment, but I did not have enough reputation to do so (I find it weird btw, to not be able to comment because you're too junior).

[Edit2] pthread seems a valid solution! (which I have not tried so I can't advise)

7 Comments

Could you give some reference, about your recommendation, that I can follow?
It is not possible to use the function 'pcntl_fork' when PHP is used as Apache module. You can only use pcntl_fork in CGI mode or from command-line.
What do you mean "not possible"? As in "will not work as expected"? or as in "function not defined in this context"? because if the latter, it is simply a matter of properly enabling it in PHP.ini. However, indeed, it is not recommended because it will fork the Apache worker and this is not what you want to do.
Yes, it reads there "undefined function", but in fact it only depends on your php.ini, and in particular the disable_functions property. You could perfectly enable it and... take your whole system down in minutes :)
Gabriel has a good answer because it explains something which makes sense - because forking will fork the Apache worker thread NOT just the php process.I can see why that isn't what was wanted. I suspect Richard really wanted a way to run a specific function as a background thread, as you can in many other languages, which doesn't involve forking a copy of the entire process. Not everyone uses LAMP systems for public web services, we use them for engineering systems on intranets. Sometimes all you want is a set of steps all doing in parallel if there are spare cores to run them.
|
0

The idea of "thread safe" can be very broad. However PHP is on the very, furthest end of the spectrum. Yes, PHP is threadsafe, but the driving motivation and design goals focus on keeping the PHP VM safe to operate in threaded server environments, not providing thread safe features to PHP userspace. A huge amount of sites use PHP, one request at a time. Many of these sites change very slowly - for example statistics say more sites serve pages without Javascript still than sites that use Node.js on the server. A lot of us geeks like the idea of threads in PHP, but the consumers don't really care. Even with multiple packages that have proved it's entirely possible, it will likely be a long time before anything but very experimental threads exist in PHP.

Each of these examples (pthreads, pht, and now parallel) worked awesome and actually did what they were designed to do - as long as you use very vanilla PHP. Once you start using more dynamic features, and practically any other extensions, you find that PHP has a long way to go.

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.