1

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.

Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230
  • If the request is doing IO, what else is it doing while the IO is going on? Why not give a story of what is happening in PHP and see how you may approach it to take advantage of what PHP offers. – James Black Nov 15 '09 at 15:25
  • 1
    In ASP.NET, each request is handled in one thread. Since threads as resources are limited, we don't want threads to be blocked on I/O operation. And, thread pool size is always limited in ASP.NET. So, Begin/End patterns are necessary. I suppose it is different story for PHP. Each request is handled in single process? and it doesn't matter to fork process to handle requests. So, this Begin/End asynchronous programming model is not necessary. – Morgan Cheng Nov 16 '09 at 02:12

6 Answers6

1

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
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

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
  • 5,454
  • 4
  • 43
  • 67
0

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
  • 6,680
  • 1
  • 44
  • 46
0

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
  • 4,317
  • 1
  • 32
  • 24
0

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
  • 70,219
  • 68
  • 205
  • 290
0

Using stream_select you can create child processes via a HTTP request. Checkout the code in http://drupal.org/project/httprl for some ideas on how to do this. I plan on pushing this library to github once I get it more polished; something that can be ran outside of drupal. But for now it lives in Drupal land.

mikeytown2
  • 1,744
  • 24
  • 37