English 中文(简体)
Does PHP support asynchronous programming?
原标题:

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.

最佳回答

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.

问题回答

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

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

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

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

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.

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签