views:

145

answers:

3

Can do use ajax to achieve true multi-threaded?if can ,how to do? please give me some related information,web site or books. thanks ~!

+4  A: 

It depends on what you mean by "multithreaded".

Javascript code is distinctly singlethreaded. No Javascript code will interrupt any other Javascript code currently executing on the same page. An AJAX (XHR) request will trigger the browser to do something and (typically) call a callback when it completes.

On the server each Ajax request is a separate HTTP request. Each of these will execute on their own thread. Depending on th Web server config, they may not even execute on the same machine. But each PHP script instance will be entirely separate, even if calling the same script. There is no shared state per se.

Now browsers typically cap the number of simultaneous Ajax requests a page can make on a per host basis. This number is typically 2. I believe you can change it but since the majority of people will have the default value, you have to assume it will be 2. More requests than that will queue until an existing request completes. This can lead to having to do annoying things like creating multiple host names like req1.example.com, req2.example.com, etc.

The one exception is sessions but they aren't multithreaded. Starting a session will block all other scripts attempting to start the exact same session (based on the cookie). This is one reason why you need to minimize the amount of time a session is opened for. Arguably you could use a database or something like memcache to kludge inter-script communication but it's not really what PHP is about.

PHP is best used for simple request processing. A request is received. It is processed and a response is returned. That response could be HTML, XML, text, JSON or whatever. The request could be an HTTP request from the browser or an AJAX request.

Each of these request-response cycles should, where possible, be treated as separate entities.

Another technique used is long-polling. An HTTP request is sent to the server and may not return for a long time. This is used for Web-based chat and other "server push" type scenarios. Sometimes partial responses will be flushed without ending the request.

The last option (on Unix/Linux at least) is that PHP can spawn processes but that doesn't seem to be what you're referring to.

So what is it exactly you're trying to do?

cletus
Perhaps he's under the assumption that one has to wait for an ajax request to complete before another can get fired? If so, if a function fires 5 ajax requests one after the other, it won't wait for request one to come back before processing the second.. it won't fire all 5 at the _exact_ same time, as mentioned, but they will all run within a few milliseconds of each other typically
Dan Heberden
This is a good answer, but it's worth noting that most browsers will cap the number of requests you can send out at two (although this can be changed in the browser settings), and will queue the rest, so you may not be able to get significant speedup from making parallel requests.
Dan Monego
@Dan oh I was meaning to put that in but forgot. Thanks for bringing it up.
cletus
+1  A: 

You can't actually multi-thread but what a lot of larger websites do is flush the output for a page and then use Ajax to load additional components on the fly so that the user sees content even while the browser is still requesting new information. Its a good technique to know but, like everything else, you need to be careful how you use it.

Jarrod
A: 

Ajax in Action

luochong
reading this book
luckydeng