views:

212

answers:

7
+4  Q: 

Threads in PHP.

Hello..

I am creating a web application using zend, here i create an interface from where user-A can send email to more than one user(s) & it works excellent but it slow the execution time because of which user-A wait too much for the "acknowledged response" ( which will show after the emails have sent. )

In Java there are "Threads" by which we can perform that task (send emails) & it does not slow the rest application.

Is there any technique in PHP/Zend just like in Java by which we can divide our tasks which could take much time eg: sending emails.

Thanks..

A: 

PHP doesn't include threading as part of the language, there are some methods that can emulate it but they aren't foolproof.

This Google search shows a few potential workarounds

Neil Aitken
A: 

You could look into using multiple processes, such as with fork. The communication between them wouldn't be as simple as with threads (but then, it won't come with all of its pitfalls either), but if you're just sending emails, it might not be necessary to communicate much, if at all.

Doug Kavendek
A: 

As of PHP there are no threads in it. However for php, you can have a look at this roundabout way http://www.alternateinterior.com/2007/05/multi-threading-strategies-in-php.html

manugupt1
A: 

There is pcntl, which allows you to create sub-processes, but php doesn't work very well for this kind of architecture. You're probably better off creating a long-running script (a daemon) and spawning multiple of them.

troelskn
A: 

You may want to use a queue system for your email sending and send the email from another system which supports threads. PHP is just a tool and you should the tool that is best fitted for the job.

Elzo Valugi
+6  A: 

No threads in PHP!

The workaround is to store jobs in a queue (say rows in a table with the emails) and have a cronjob call your php script at a given interval (say 2 minutes) and poll for jobs. When jobs present fetch a few (depending on your php's install timeout) and send emails.

The main idea to defer execution:

  • main script adds jobs in the queue
  • cron script sends them in tiny slices

Gotchas:

  • make sure u don't send an email without deleting from queue (worst case would be if a user rescieves some spam at 2 mins interval ...)
  • make sure you don't delete a job without executing it first ...
  • handle bouncing email using a score algorithm
clyfe
+1. This is how most mailing list systems work that are using PHP.
snicker
A: 

Watch out for doing forks on an Apache process. You may get some behaviors that you are not expecting. If you are looking to do any kind of asynchronous execution it should be via some kind of queuing mechanism. Gearman is one. Zend Server Job Queue is another. I have some demo code at Do you queue? Introduction to the Zend Server Job Queue. Cron can be used, but you'll have the problem of depending on your cron scheduler to run tasks whereas asynchronous computing often needs to be run immediately. Using a queuing system allows you to do that without threading.

Kevin Schroeder