Threads in PHP?

EDIT (thanks @Efazati, there seems to be new development in this direction)

http://php.net/manual/en/book.pthreads.php
Caution: (from here on the bottom):

pthreads was, and is, an experiment with pretty good results. Any of its limitations or features may change at any time; […]

/EDIT

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

Leave a Comment