thread in PHP

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
lipun4u
Forum Commoner
Posts: 82
Joined: Wed Jul 01, 2009 3:35 am
Location: Mumbai
Contact:

thread in PHP

Post by lipun4u »

I have a class which takes an array of data and processes it and returns it. For huge amount of data, it takes a lot of time.

I want to implement this in threads, but PHP has no threading library.

Can anyone help me to fix this ?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: thread in PHP

Post by Christopher »

PHP is a technology embedded in a web server, so long running processes like this are probably best done elsewhere. You could, of course, just increase the connection timeout just for this purpose. Or you could get a faster server to reduce the time.

I would recommend running a separate program on the server to process this data. This could be in CLI PHP or some other language. There are technologies like Gearman that might help you with this.
(#10850)
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: thread in PHP

Post by tr0gd0rr »

Here is some code that uses `popen` and `pclose` to start a process from the command line and not wait for it to exit.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: thread in PHP

Post by Chris Corbyn »

If you are running this code on a POSIX server (i.e. not Windows) then you probably want to use a combination of pcntl_fork() and pcntl_signal(). I believe it's useful to learn to use these UNIX concepts in any case, but they are quite difficult to get to grips with, especially if you need to share data between the running processes (which you would need to do with a socket pair, a TCP connection or a uni-directional pipe).

EDIT | I agree with Christoper, just to be clear ;) Don't try to force a browser to wait a long time for a long-running process. Background that work as much as possible. If you need to do background work on-demand, rather than via cron, look at a queueing system such as beanstalk (in combination with pheanstalk), or php-resque, in combination with Redis.
Post Reply