How to efficiently manage multiple threads with PHP/Apache
Posted: Fri Jun 01, 2007 12:22 pm
I'm currently writing various aspects of a web application that I can't really release the details of (NDA)
.
Anyway, let me set the scene. Say I want to write an Ajax-based web front-end that changes regularly based on business logic running on the server. This is usually accomplished by polling the server on some interval, using Ajax. Google does this with it's iGoogle widgets. Unfortunately though, in relative terms, we want a *much* shorter polling interval than iGoogle's.
So I figured I'd write a generic Ajax/process management thingy. A client - let's call it Bob - would send an Ajax request to the server, which would log the sessionId and sleep the "thread". Whenever the server decided to communicate with Bob, it would stuff whatever it needed into Bob's session and wake Bob's sleeping server thread. Bob's thread would return the Ajax request to him. The Ajax response would be parsed and Bob's display would be changed appropriately. Bob would then send an Ajax request exactly the same as the first one, and that would be put to sleep, and then ... and the process would repeat.
Hopefully, my issue is somewhat apparent by this point. I know that when 50, 000 users are logged into the site and are all communicating with the server, Apache is essentially running some kind of thread for each request. How might I go about manipulating these threads so I can sleep, wake, or signal them? And if there are limitations, what's the best way to go about doing what I want to do. I'm not entirely sure if this is the best place for this post. I placed my question here because I figure this could expand into interesting directions i.e. blocking a thread that needs to hear back from an external process that is managed by the PHP code in Apache. Or maybe theres just some simple way to manage threads .
I'll appreciate any advice on this matter.
Graham
Anyway, let me set the scene. Say I want to write an Ajax-based web front-end that changes regularly based on business logic running on the server. This is usually accomplished by polling the server on some interval, using Ajax. Google does this with it's iGoogle widgets. Unfortunately though, in relative terms, we want a *much* shorter polling interval than iGoogle's.
So I figured I'd write a generic Ajax/process management thingy. A client - let's call it Bob - would send an Ajax request to the server, which would log the sessionId and sleep the "thread". Whenever the server decided to communicate with Bob, it would stuff whatever it needed into Bob's session and wake Bob's sleeping server thread. Bob's thread would return the Ajax request to him. The Ajax response would be parsed and Bob's display would be changed appropriately. Bob would then send an Ajax request exactly the same as the first one, and that would be put to sleep, and then ... and the process would repeat.
Hopefully, my issue is somewhat apparent by this point. I know that when 50, 000 users are logged into the site and are all communicating with the server, Apache is essentially running some kind of thread for each request. How might I go about manipulating these threads so I can sleep, wake, or signal them? And if there are limitations, what's the best way to go about doing what I want to do. I'm not entirely sure if this is the best place for this post. I placed my question here because I figure this could expand into interesting directions i.e. blocking a thread that needs to hear back from an external process that is managed by the PHP code in Apache. Or maybe theres just some simple way to manage threads .
I'll appreciate any advice on this matter.
Graham