Passing some of the proccessing burden to another PHP file

I had a php script which updated a mySQL table and then ran a select query to return values back to a FLASH AS3 page. Everything was working just dandy until I incorporated email notification with PHP mailer. Now the script runs too slow and is causing issues further down the line so, I’m to move the burden of this email notification to its own php file and thus remove the delay. I’ll need to pass a varaible to this new page but my question is - what is the best approach to achieve this, to pass the variable and the proccessing overhead to this new php file I’m looking to create? Guidance on this would be very much appreciated.

I’ve looked into using INCLUDE but not sure it this removes the burden from the current php file.

as suspected, it doesn’t.

you could try a (AMQ) queue like (e.g.) RabbitMQ.

This is a typical issue of the synchronous nature of standard PHP scripts. What you need to look for is either a queuing system like Dormilch mentions or a threading system, which allows functions to run asynchronously, like with pthreads or reactphp.

Another possibility is to make your own type of queuing system with a database for the queue information and a cron script, which calls another PHP script to work off the queue at given times, like sending off batches of emails every few minutes.

It all depends on what your requirements are.


@Dormilich - Thanks buddy, I was affraid that might have been the case…damn!
@s_molinari - Thanks for the steer Scott - making my own queing system is a little (lot) out of my league. Hmmm, looks like I may have to re-write my AS3 function - Oh joy…
Thanks for the guidance both of you thought the end result was not quite what I’d envisaged…

It isn’t that hard. Instead of sending the email off to the mail() function, you store the data in the database. Then you create a PHP script, which calls up the open emails in the database and sends them off and deletes the records in the database. Then you make a cron script to call the mail sending PHP script every minute or so.

With a quick search I found this.

I don’t know if the tutorial is good or not, but if you search for “php email cron script”, you’ll find enough examples.


@s_molinari - thanks for your addtional help… and another good steer… I used php email notification shedule script and got some interesting results… Never occured to create a separtae scheduler… thanks again… If I get success I’ll post the results so that, hoepfully, others may benefit

I was tasked with something similar and previous devs tried everything to make it work without delay.
I also posted this question here and was told it was not possible.
“not possible” is not in my vocabulary.
This is what I came up with, it was about 6 years ago and hope it may help you.
copied verbatim from a hand off to salesforce script, salesforce.php that slept for 5 minutes

// added by loren
$a = $SAVE_DATA['leads_email'];
$b = $SAVE_DATA['leads_f_name'];
$c = $SAVE_DATA['leads_l_name'];
$d = $SAVE_DATA['fk_admin'];
//$d = '30';
$e = $SAVE_DATA['leads_datetime'];
$f = $SAVE_DATA['fk_site'];
$g = 'khjsdfkjhsfiyiuyraa';

exec("nohup nice php -q ./salesforce.php  -a $a -b $b -c $c -d $d -e $e -f $f -g $g > /dev/null & echo $!");

then to get the data in salesforce.php.

$arg = getopt("a:b:c:d:e:f:g:");

$lemail = $arg['a'];
$lfname = $arg['b'];
$llname = $arg['c'];
$admin = $arg['d'];
$date = $arg['e'];
$site = $arg['f'];
$action = $arg['g'];

I needed this for a two part form, once part 1 is completed part two may or may not get filled out.
The main script web page has no delay while salesforce.php waits 5 minutes and then checks to see if part 2 was filled out and then sends what it has to

@lorenw - I noted others suggesting exec() but didn’t say anything just in case some one thought I knew what I was talking about( which I don’t) Is it true that exec cant be run on a windows machine?

Apologies in advance for the lengthy response but just to clarify: My flow is like this - AS3 sends a request to ‘check status’ . There’s no forms involved. Firstly, the PHP script updates a table to ensure the latest status, then it does a SELECT on the updated row to return data to AS3. At the same time(in the background so to speak), myPHP script evaluates the current status and, based on that status value, creates the email message to be sent. Once the message is created, I run a SELECT query to get the right recepients and when I get the result set back this is where PHP Mailer kicks in. Unfortunately, mailer also slows my script execution time down enough for AS3 to question where the hell the response got to and therefore fires off a send request again. Of course, once it fires again so does the mailer, so there are scenarios where recipients may get the same email twice if the script takes too long to run. I’ve commented the mailer bit out to prove that this is the bottleneck and it is, hence my question in the first place. It sounds like your solution has legs and hopefully, by my narrative, you’ll agree that your solution would fit but I feel as though I am getting into waters deeper than I can swim in here. I’m not a developer and I’ve never used anything like this so, gulp, it seems a bit daunting…

Mail should not be causing noticeable delay - sending a mail command is akin to sticking the message in an Outbox tray - you drop it off, and the person who does the mail comes along later and picks it up. The script doesn’t sit there and stare at the email until the person comes to collect it.

exec() is not OS-exclusive, but the commands you send through it certainly will be.

@StarLion Thanks for pointing out, in the nicest way, that “a script doesn’t sit there and stare at the email until a person comes to collect it”… I think I grasp the general concept, as though it were jelly in my hands…
Prior to posting I did some checks with a web proxy, with the mail function commented out the response time was circa 20ms and everything was great… with the mail function running it was 1.29 seconds and I beleive my AS3 script is firing off the send request a second time before it gets a reply from the first request… or perhaps the delay is with my hoster, I don’t know yet but I’d like to get to the bottom of it. At present its not a show stopper and having little to no experience with the exec function further research is undoubtidly required but thats for your gaphical, somewhat witty portrayal of how mail works. It was a great help

If you’re truly that concerned about just the mailing function, use it as a queue system.

Have your script stick the “to be mailed” email (or just the address, or whatever) into a database, or a file, or something like that.

Assuming you have control over timed events on your host (if you don’t, there are ways around it, but it’s easier if you do), every… 5 minutes or so, run an independent script that runs and picks up all waiting emails, processes them in your mailer, and then removes the entry from wherever you stored it.

That way, your AS3-responder isnt waiting on external programs, it’s just doing a file/database write and moving on.

@StarLion - Thanks for the input… Its not that I am truly concerned but I would like to understand more, in the hope that it will reduce the number of arguably stupid qestions I ask. eMial notifications need to be sent pretty quickly so unfortunately can’t be scheduled as you suggest. That said, writing the recipients and the email message etc to a db table as you and s_molinari suggest might be a good option. I could then get AS3 to push out a send request a few seconds after it receives its initial response for the first send request… Thanks again for getting back to me…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.