views:

301

answers:

5

I'm new to PHP. I am familiar with ASP.NET which support asynchronous programming. That is, if one request needs to do some I/O job. It is suggested to program the web page with BeginProcess/EndProcess way. The asynchronous programming is key to improve scalability.

I'm wondering whether there is counterpart of asynchronous programming(BeginXXXX/EndXXXX) in PHP world.

+1  A: 

In .NET BeginXXX/EndXXX paradigm relies heavily on threading, while on PHP I am not sure that you could even start a new thread (except maybe the PECL package).

FastCGI is the alternative to multithreading in most interpreted languages. Instead of spawning new threads it uses processes, but as spawning a new process is expensive, it keeps a reusable process pool just as the ThreadPool in .NET.

Darin Dimitrov
Sad news. So, what we can do is to fork process?
Morgan Cheng
Yes you can fork processes but as this is an expensive operation I am not sure that you will get much performance improvements. Of course, as always you might have to measure.
Darin Dimitrov
I suppose forking processes should be light weighted in *nix OS.
Morgan Cheng
You don't need multi-threading for asynchronous programming.
mixdev
A: 

The core has a set of process control functions, including the ability to fork a process. I don't know that I'd use these in a web script, but have used them in command line scripts before.

http://www.php.net/manual/en/book.pcntl.php

http://www.php.net/manual/en/pcntl.example.php

txyoji
+1  A: 

If the I/O is performed with sockets or files you should use stream_socket_select() or stream_select() respectively (similar to system calls in C/C++).

Here's a simple command line chat tutorial done with PHP: Simple PHP socket-based terminal chat

Note: This is not a general multi-threading solution, but a simple solution for situations where you need "semi-parallel" I/O

MeLight
A: 

Here's an interesting link on the subject of PHP multiplexing with PHP4 and PHP5 samples:

http://netevil.org/blog/2005/may/guru-multiplexing

Barnabas Kendall
A: 

PHP doesn't, but you could use AJAX once the page has loaded, which will allow asynchronous requests.

Honestly though, there is no point. If you really want that heavyweight of a back end, you're better off writing a separate program that does the heavy lifting. PHP modules are written in pure C as far as I'm aware, so you should be able to use that and then call your own custom function from PHP.

DisgruntledGoat