How to use semaphone with PHP
Semaphore is a protected variable that can be used to control access to a resource. I’ve used a semaphore to prevent a PHP script from running simultaneously. For example I have a PHP script that runs every Tuesday to process a weekly newsletter. This script first selects a list of recipients to send. The script then sends each recipient a copy of the newsletter. The list is more than 10,000 subscribers and each subscriber is processed individually in order to customize the newsletter content. As each subscriber is processed the script updates the database for that subscriber with a last sent date of today. If the script runs a second time the subscribers will not be selected since they were updated with a last sent date of today. However if the script is run a second time before the first instance completes the initial list is repopulated and the newsletter is sent twice. To prevent the script from running simultaneously I use a semaphore to “lock” the process.
Continue reading “PHP Semaphore Example”