Run Scripts in Separate Threads in PHP

I’ve been working on a new project over the past few weeks and one of the requirements of the system was to update a number of records in the database based off the data stored in a large quantity of XML files that would be stored on an external CDN. While initially this didn’t seem a problem, once the number of records grew that were required to be updated it became obvious that doing the updates sequentially wasn’t going to work, and as I didn’t have the required extensions to use pcntl-fork I had to come up with another way to launch multiple updates simulatenously, which turned out to be much simpler than I had thought but took a bit of searching to find:

<?php
	exec("nohup /usr/local/bin/php -f /path/to/script/file.php parameterValue &gt; /dev/null &amp;");
?>

This will execute the PHP script stored in file.php in its own thread but the PHP script calling it will not have to wait on it finishing execution as it would be if you called the script without the nohup and > /dev/null &. This solution will only work on *nix systems as opposed to Windows, but I imagine there is probably a way to do it in Windows too. The downside to this is that you will not be able to get any return value from the script, therefore it is only of any use if you need to run a task that does not require the calling file to get the results back. You can however pass data to the script as I have done in the example above, the value “parameterValue” would be available to file.php via $argv[1], for example:

<?php

	/* -- file.php -- */
	echo 'The value '.$argv[1].' was passed to me via a CLI command.';

?>
Posted in Programming, Web Development at September 6th, 2011. No Comments.

phpAnalyzer Goes Live!

phpAnalyzerAfter many months of analysis, research, design, development and testing I have decided to open phpAnalyzer to the public. Up until this point I have been very secretive about the system as I produced it in partial fulfilment of my BSc and didn’t want anyone with more resources and time to be able to spring up a similar system overnight that voids the usefulness of my own; now however I can launch it and I hope that it will be of use to people and meet the goals it was originally designed to conquer.

Check out phpAnalyzer at: http://www.phpanalyzer.co.uk/

What is phpAnalyzer?

phpAnalyzer allows users to upload a script to the system, at which point it will scan the script for bad PHP development practise; once the file has been fully scanned a report will be displayed to the user which indicates what they need to revise and display possible solutions to the problems, which in most cases will contain links to the appropriate pages in the online PHP manual which will aid the user in implementing the updated and correct methods and or global objects.

Who is phpAnalyzer aimed at?

It is primarily aimed towards students / people learning PHP as with an ever growing number of students and freelance developers turning to online resources it is important that a tool be available that will allow the developer-to-be to ensure what they are reading about is indeed the way they should be tackling a given problem.

Posted in Software, Technology, Web Design, Web Development at May 25th, 2010. 2 Comments.