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 ?
thread in PHP
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: thread in PHP
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.
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)
Re: thread in PHP
Here is some code that uses `popen` and `pclose` to start a process from the command line and not wait for it to exit.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: thread in PHP
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.
EDIT | I agree with Christoper, just to be clear