background preloader

Asynchronous Php

Facebook Twitter

Asynchronous background execution with PHP « shapeshifter.se. If you ever worked with threads and particular work queues you know how convenient they can be. Have some demanding work that needs to be done but no time to do it yourself? No problem, just put it on the work queue and continue with whatever we were doing, some other thread will come along and do the dirty work for you. Consider the following scenario. Actions and inputs from a web page triggers something that might take a (very) long time to execute and if executed during the browser session which besides annoying the user who has to wait for a page to load, might cause a time out and interrupt the processing. How do we solve this? I’ll use the term job to indicate some work that needs to be execution, in practice this is an isolated PHP function which takes an unspecified time to execute.

Some possible solutions Store job information somewhere (like a SQL database) have a separate script, PHP or in some other language, running on the host, either continuously or from crontab. Using it. Proc_open. Pcntl_fork. Using pcntl_fork() can be a little tricky in some situations. For fast jobs, a child can finish processing before the parent process has executed some code related to the launching of the process. The parent can receive a signal before it's ready to handle the child process' status. To handle this scenario, I add an id to a "queue" of processes in the signal handler that need to be cleaned up if the parent process is not yet ready to handle them.

I am including a stripped down version of a job daemon that should get a person on the right track. <? Public $maxProcesses = 25; protected $jobsStarted = 0; protected $currentJobs = array(); protected $signalQueue=array(); protected $parentPID; public function __construct(){ echo "constructed \n"; $this->parentPID = getmypid(); pcntl_signal(SIGCHLD, array($this, "childSignalHandler")); } public function run(){ echo "Running \n"; for($i=0; $i<10000; $i++){ $jobID = rand(0,10000000000000); The Mysteries Of Asynchronous Processing With PHP - Part 3: Impl.

You can read more about Mockery at Mockery 0.6.1 includes a functional fix which ensures mocking classes containing variants of the __call() method with or without typehinting are correctly mocked/replaced. I have also downgraded the PHP dependency to 5.3.0 from 5.3.2 by request. Thanks to everyone who so far has offered feedback! Mockery has been downloaded a total of 274 times since it’s original release. Counting those of you doing it twice or three times on differing machines, that probably means around 100 or more people have installed Mockery (at a guess). Remember we have a mailing list if you wish to ask any in-depth questions, you can report issues or feature requests on Github, and I’m usually somewhere on IRC (and Twitter) in the evening times (GMT).

Related posts: Blog > How To Run PHP Code In The Background. If you have php code that would take longer than 5 minutes to run, what would you do? You could update the configuration file for apache, or whatever web server you're running, to increase the amount of time a script page is allowed to run and then use php's ini_set("max_execution_time", seconds) to increase the amount of time php will allow a script to run. Or you could use a session variable to mark the last position of the script and then use meta-refresh to start the script at the last position.

Or you could try an asynchronous call by using php's exec command. Asynchronous Example Here's an oversimplified example. Let's say that you've been charged with resizing all the images in the Products folder. Now what? Resize_exec.php <%$command = "/usr/bin/php4 -f /var/www/myweb/image_resize.php";exec( "$command > /dev/null &", $arrOutput );%> $command is a linux/debian command for running a php page through a command line. Your image_resize.php script is now able to run completely to the end.

eZcomponents