<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>High Fibre Programming &#187; PHP</title>
	<atom:link href="http://www.4pmp.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.4pmp.com</link>
	<description>PHP, MySQL, C, Java, Linux and other great after dinner speech topics</description>
	<lastBuildDate>Tue, 17 Jan 2012 09:10:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Daemonize PHP (properly)</title>
		<link>http://www.4pmp.com/2011/02/daemonize-php-properly/</link>
		<comments>http://www.4pmp.com/2011/02/daemonize-php-properly/#comments</comments>
		<pubDate>Sun, 06 Feb 2011 21:39:35 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[background]]></category>
		<category><![CDATA[daemon]]></category>
		<category><![CDATA[multiprocessing]]></category>
		<category><![CDATA[multitasking]]></category>

		<guid isPermaLink="false">http://www.4pmp.com/?p=403</guid>
		<description><![CDATA[There is often a need when building a website to automate background processes, for example: Sending emails: many sites don&#8217;t immediately connect to an SMTP server when a user fires off an email request, but rather store the email in the database which is processed by a background process, thus decoupling the frontend from the [...]]]></description>
			<content:encoded><![CDATA[<p>There is often a need when building a website to automate background processes, for example:</p>
<ul>
<li><strong>Sending emails:</strong> many sites don&#8217;t immediately connect to an SMTP server when a user fires off an email request, but rather store the email in the database which is processed by a background process, thus decoupling the frontend from the SMTP server.   If the SMTP server goes down, your users can still register</li>
<li><strong>Offline processing:</strong> let&#8217;s say your site has a social element to it, users have real-time news feeds that get updated whenever their friends do things on your site.   By updating these feeds offline there&#8217;s no need for users to wait until all 1,000,000 of their friends&#8217; feeds have been updated before completing a request to post a simple comment.</li>
</ul>
<p>There are plenty more examples, in fact as a rule of thumb it&#8217;s best to do everything that doesn&#8217;t have to be done immediately on a HTTP request later in the background.   It also helps manage peak traffic, when your site is overloaded, just don&#8217;t run the background tasks until the peak has gone.   Ok, so news feeds aren&#8217;t bang up-to-date for a short period, but at least the site works.   So how does one go about automating all these background tasks?</p>
<h3>CRON</h3>
<p>This is probably the first port-of-call; implement your background task in PHP, thereby enabling you to use the database abstraction layer and all the other libraries in your application, and set CRON to run the script every minute.</p>
<p>Great – everything works perfectly, that is until the data in your database grows so big that the tasks start taking longer than one minute and you end up with lots of tasks all running at the same time – and potentially all processing the same data!   Ooops, the boss just got his registration email ten times.</p>
<p>So you could implement some sort of locking mechanism to prevent multiple instances but it&#8217;s all starting to get a bit fragile with lots of interdependencies – all you want to do is run a simple script!</p>
<h3>Write a daemon in PHP</h3>
<p>So it&#8217;s back to the drawing board – what is needed is a daemon that will sit in the background and run as script continually, restarting it immediately after it finishes.   You could write a daemon in PHP – this would enable you to write a daemon which would interface well with your existing PHP scripts, there are even some good PHP packages which make writing a daemon in PHP easy.</p>
<p>So, full of enthusiasm you launch into writing your own daemon in PHP.   A week or so later, after reading all about process control, (and probably pulling large chunks of hair out in the process as yo realise it wasn&#8217;t as simple as you thought), you finally have something that works.   You deploy it to your website, sit back and feel smug.</p>
<p>After a week your boss rages into your office demanding to know why nobody has registered in the past week.   Red faced, you go to check your PHP daemon that sends emails form the email queue is still running – and it isn&#8217;t.   If you haven&#8217;t already been fired then your next step would probably be to find out what happened.   Looking through your PHP error log you find a “FATAL ERROR” from your daemon script, dated exactly one week ago.</p>
<p>PHP can throw fatal errors for all sorts of reasons, and there&#8217;s no way to recover from them.   Also, PHP is renowned for its rather flaky <a title="PHP Garbage Collection" href="http://www.php.net/manual/en/features.gc.performance-considerations.php">memory management</a> – whilst this is fine for web scripts that render a page and then end, it makes it highly unsuitable to write applications.</p>
<h3>Enter The Fat Controller</h3>
<p>How can we solve the problem?   We need to run PHP scripts as a daemon.   I faced this problem one year ago and I solved it by writing The Fat Controller.   It is a program written in C that runs a daemon and can continually run PHP scripts.   As it is written in C, it is highly stable and can run for months or years without problem.   As the daemon runs separately from the PHP scripts, no matter what happens in the PHP script, it does not affect the Fat Controller.</p>
<p>The Fat Controller is also very flexible, easy to install and configure and supports running multiple instances of a PHP at the same time to achieve parallel processing.   You can even configure it to dynamically control the number of parallel processes dependent on how much work there is.</p>
<p>You can read more about The Fat Controller here   <a title="The Fat Controller" href="http://www.4pmp.com/fatcontroller/">http://www.4pmp.com/fatcontroller/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.4pmp.com/2011/02/daemonize-php-properly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programmatically set environment for Symfony project</title>
		<link>http://www.4pmp.com/2010/10/programmatically-set-environment-for-symfony-project/</link>
		<comments>http://www.4pmp.com/2010/10/programmatically-set-environment-for-symfony-project/#comments</comments>
		<pubDate>Wed, 20 Oct 2010 08:10:59 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[environment]]></category>
		<category><![CDATA[subversion]]></category>
		<category><![CDATA[svn]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://www.4pmp.com/?p=335</guid>
		<description><![CDATA[I have recently been working on a web project built on Symfony 1.4. There are a team of developers, each has their own installation and we had the problem whereby each needed their own particular configuration settings. Symfony allows for multiple configurations to be specified using “environments” but the problem is how to get each [...]]]></description>
			<content:encoded><![CDATA[<p>I have recently been working on a web project built on Symfony 1.4.   There are a team of developers, each has their own installation and we had the problem whereby each needed their own particular configuration settings.   Symfony allows for multiple configurations to be specified using “environments” but the problem is how to get each installation to choose the correct environment without changing index.php &#8211; which of course would affect everyone else each time it was committed to Subversion.</p>
<p>The solution was to create a simple class that looks for the existence of a file, environment.cfg, in the configuration directory.   The contents of the file specify which environment to use.   This file is not committed to Subversion each developer can specify their own environment without affecting anything in Subversion.</p>
<p>We added a few lines to our index.php file like this:</p>
<pre class="brush: php;">
// Load the class which chooses the environment
require_once(dirname(__FILE__).'/../lib/helper/ApplicationEnvironment.class.php');

// Instantiate the class - specify the default environment to &quot;live&quot; (if there is no environment.cfg file)
$environment = new ApplicationEnvironment(&quot;live&quot;);

// Try and read environment.cfg
$environment-&gt;overrideFromFile(dirname(__FILE__).'/../config/environment.cfg');

// Create a configuration object using the chosen environment
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', $environment-&gt;getEnvironment(), $environment-&gt;getDebugMode());
</pre>
<p>Note that the debug mode option is also set by the ApplicationEnvironment object.   This is determined by the URL:</p>
<pre class="brush: php;">
// in ApplicationEnvironment.class.php
    public function getDebugMode()
    {
        $debugAddresses = array(&quot;debug.example.com&quot;);

        return in_array($_SERVER['SERVER_NAME'], $debugAddresses);
    }
</pre>
<p>We could have used the environment.cfg file to specify this, for example on another line, but we also needed to specify debug mode on our live server so that if the site is accessed at www.oursite.com it is not in debug mode, but if it is accessed via debug.oursite.com then it is in debug mode (of course debug.oursite.com is http password protected).</p>
<h2>CLI</h2>
<p>Next we had the problem of setting the environment.   We could of course rely on each developer to put <code>--env=my-environment</code> but it would be much nicer to have this done automatically.   The solution was to create a simple script that resides in the application root directory next to symfony.php that looks something like this:</p>
<pre class="brush: php;">
require_once(dirname(__FILE__).'/lib/helper/ApplicationEnvironment.class.php');

$environment = new ApplicationEnvironment(&quot;live&quot;);
$environment-&gt;overrideFromFile(dirname(__FILE__).'/config/environment.cfg');

$envSet = false;

foreach ($argv as $i =&gt; $arg)
{
    if (substr($arg, 0, 6) == &quot;--env=&quot;)
    {
        $envSet = true;
        break;
    }
}

if (!$envSet)
{
    $argv[] = sprintf(&quot;--env=%s&quot;, $environment-&gt;getEnvironment());
    $argc++;

    $_SERVER['argv'] = $argv;
    $_SERVER['argc'] = $argc;
}

chdir(dirname(__FILE__));
require_once(dirname(__FILE__).'/config/ProjectConfiguration.class.php');
include(sfCoreAutoload::getInstance()-&gt;getBaseDir().'/command/cli.php');
</pre>
<p>It&#8217;s a bit of a nasty mess, but it works at least.</p>
<h3>Download</h3>
<p><a href='http://www.4pmp.com/wp-content/uploads/2010/10/ApplicationEnvironment.class.php_.gz'>ApplicationEnvironment.class.php</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.4pmp.com/2010/10/programmatically-set-environment-for-symfony-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multitasking: PHP in parallel</title>
		<link>http://www.4pmp.com/2010/03/multitasking-php-in-parallel/</link>
		<comments>http://www.4pmp.com/2010/03/multitasking-php-in-parallel/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 22:03:13 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[multitasking]]></category>
		<category><![CDATA[multithreading]]></category>
		<category><![CDATA[wrapper]]></category>

		<guid isPermaLink="false">http://www.4pmp.com/?p=257</guid>
		<description><![CDATA[Update: I have recently solved the issue of multitasking in PHP with a standalone application, The Fat Controller, which handles multitasking for you. Read more here. Like most websites, glogster.com periodically sends out an e-newsletter to its user-base.    This is done by a simple PHP script that opens a socket to a mail server and [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> <em>I have recently solved the issue of multitasking in PHP with a standalone application, <a href="http://www.4pmp.com/fatcontroller/">The Fat Controller</a>, which handles multitasking for you.   Read more <a href="http://www.4pmp.com/fatcontroller/">here</a>.</em></p>
<p>Like most websites, glogster.com periodically sends out an e-newsletter to its user-base.      This is done by a simple PHP script that opens a socket to a mail server and goes through the user database, sending an email to each one.      This approach has worked fine until recently, the number of users has gone up dramatically and now it takes an infeasibly long time to send emails to all our users.</p>
<p>After optimising the script as much as possible, the only alternative option was to run many instances of the script simultaneously.   PHP doesn&#8217;t support multithreading, and although PHP does support POSIX style process control ( <a title="PHP PCNTL Documentation" href="http://www.php.net/manual/en/book.pcntl.php">http://www.php.net/manual/en/book.pcntl.php</a> ) I decided to create a wrapper in C that would handle multiple instances of PHP.   <em>(I haven&#8217;t found anything technically wrong with PHP&#8217;s process control, I just needed something stable, reliable and potentially able to mulitask other scripts such as Python).</em></p>
<h4>How it works<em><br />
</em></h4>
<p>The wrapper I made (see below for code listing) is fairly simple and works by creating a series of threads, each of which forks and creates a PHP instance (replacing the child process) that runs the emailing script.   The thread from the parent process waits for the PHP process to finish and then ends itself.   Once a thread ends, the main thread creates another one, thereby maintaining a constant number of threads.   Signal handling is handled by a separate thread and can either tell the main thread to stop creating new threads and thereby safely shutdown the program or directly terminate all child processes and thus abruptly end the program.</p>
<h4>Almost done</h4>
<p>So now I had a way of running any number of PHP processes simultaneously and was almost ready to send emails at lightning speed, however one problem remained.   Once an email has been sent to a user, the database is updated so that I know which user has received which email and thus prevent me from sending the same user the same email more than once.   The problem is; what if one instance of the PHP script reads a user, and before it has time to send them the email and update the database, another PHP instance reads the same user and also sends them the email?   One way to solve this would be through locking the database table before reading and updating each row, however this would slow the database down which is not ideal.</p>
<p>The solution I came up with was to limit the number of simultaneous PHP instances to 8 and have the wrapper send each PHP process a unique instance ID from 0 to 7.   When selecting users from the database, the script looked at the 3 least significant bits (LSBs) of the IDs in the recipients table and selected only those rows whose 3 LSBs equated to the instance ID passed from the wrapper.</p>
<pre class="brush: sql; gutter: false;">SELECT *,
(
	if( (`id` | 1) = `id`, 1, 0) +
	if( (`id` | 2) = `id`, 2, 0) +
	if( (`id` | 4) = `id`, 4, 0)
) AS `IID`
FROM `recipients`
WHERE `sent` = 0
HAVING `IID` = :IID
LIMIT :OFFSET, :BATCH_SIZE;
</pre>
<p>Just bind the instance ID passed from the wrapper into the query and each instance will now select recipients that are unique to that ID &#8211; no two concurrent instances can select the same recipients.   Just don&#8217;t forget to update each row once each email has been sent.</p>
<p><strong>Note:</strong> <em>Using OFFSET in a limit clause is inefficient, for an alternative: <a href="http://www.4pmp.com/2010/02/scalable-mysql-avoid-offset-for-large-tables/">http://www.4pmp.com/2010/02/scalable-mysql-avoid-offset-for-large-tables/</a></em> </p>
<h4>Finishing touches</h4>
<p>We&#8217;re almost there, just one more little feature is required and the system will be perfect (sort of).   If a thread process has nothing to do then it will return immediately, so we end up with a situation when thread processes are constantly created and destroyed which is not the most efficient use of resources.   The solution was to check the <a href="http://en.wikipedia.org/wiki/Exit_status">exit status</a> of the PHP process.   If zero then all is ok, the thread ends and is available to be restarted immediately as normal.   If however, the exit status is non-zero then the thread is marked as &#8220;sleeping&#8221; and cannot be restarted by the main thread for another 30 seconds.   </p>
<p>If the PHP script doesn&#8217;t find any rows in the database to process then it returns 250 (a non-zero, non-reserved exit status) and so the calling thread in the wrapper sleeps for 30 seconds before trying again to see if there are any new items in the database to process.</p>
<p>This has the added advantage that if anything goes wrong in the PHP script, such as a fatal error, then the thread will sleep and you won&#8217;t end up with perpetuate thread cycling.</p>
<h4>Code listing</h4>
<p>Here is the code for the wrapper.   The code should be fairly straightforward, with the above notes and the inline comments you should be able to figure out what&#8217;s going on &#8211; just don&#8217;t forget to compile with the pthread library.   Comments are of course very welcome!</p>
<pre class="brush: cpp;">
#include &lt;pthread.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;signal.h&gt;
#include &lt;errno.h&gt;
#include &lt;sys/wait.h&gt;

#define NUM_THREADS 8
#define THREAD_STACK_HEADROOM 1048576

pthread_mutex_t mutexSignal;
int slots[NUM_THREADS];
int handledSignal = -1;

void* signalHandler(void* arg);
void waitForThreads();
void killall();
void *task(void *i);

/**
 * Each thread will do this
 *
 */
void *task(void *i)
{
    sigset_t signalSet;
    int iid;
    char *tid;
    int stat_loc;

    iid = (int)i;

    /* Say hello and show the thread number */
    printf(&quot;Thread %d: starting\n&quot;, iid);

    /* Spawn a child to run the program. */
    pid_t pid=fork();

    switch (pid)
    {
        case 0:
            setsid();
            sigfillset(&amp;signalSet);
            pthread_sigmask(SIG_UNBLOCK, &amp;signalSet, NULL );

            tid = malloc(11*sizeof(char));

            sprintf(tid, &quot;tid=%d&quot;, iid);

            char *argv[]={&quot;php&quot;, &quot;./sleep.php&quot;, tid, NULL};

            execv(&quot;/usr/bin/php&quot;,argv);
            free(tid);
            exit(EXIT_FAILURE); /* only if execv fails */
        case -1:
            printf(&quot;Thread %d: Fork failed\n&quot;, iid);
            slots[iid] = 0;
            break;
        default:
            /*  parent process */
            slots[iid] = pid;

            if (pid == waitpid(pid,&amp;stat_loc,WUNTRACED)) /* wait for child to exit */
            {
                printf(&quot;Thread %d: Child finished with exit code: %d&quot;, iid, WEXITSTATUS(stat_loc));

                if (WEXITSTATUS(stat_loc) != 0)
                {
                    /* Sleep this thread, set available time to now+30s */
                    printf(&quot; Going to sleep\n&quot;);
                    slots[iid] = -1 * (time(0) + 30);
                }
                else
                {
                    /* Free this thread slot */
                    printf(&quot; Returning to pool\n&quot;);
                    slots[iid] = 0;
                }
            }
            else
            {
                printf(&quot;Thread %d: Bad exit\n&quot;, iid);
                slots[iid] = 0;
            }
    }

    printf(&quot;Thread %d: Finished\n&quot;, iid);

    /* Terminate the thread */
    pthread_exit((void*) i);
}

void killall()
{
    int kv,i;

    for (i=0; i&lt;NUM_THREADS;i++)
    {
        kv = kill(slots[i], SIGINT);
        printf(&quot;Killed %d: %s\n&quot;, slots[i], kv ==0?&quot;ok&quot;:&quot;fail&quot;);
    }
}

void waitForThreads()
{
    int i, stoppedThreads;

    printf(&quot;Waiting for threads to finish\n&quot;);

    while(1)
    {
        stoppedThreads = 0;

        for (i=0; i&lt;NUM_THREADS;i++)
        {
            /* Check if thread has finished or is sleeping */
            if (slots[i] == 0 || slots[i] &lt; -1)
            {
                stoppedThreads++;
            }
        }

        if (stoppedThreads == NUM_THREADS)
        {
            break;
        }
    }
}

void* signalHandler( void* arg )
{
    sigset_t signal_set;
    int sig;

    while (1)
    {
        /* Wait for any and all signals */
        sigfillset(&amp;signal_set);

        sigwait(&amp;signal_set, &amp;sig);

        /* When we get this far, we've caught a signal */

        switch(sig)
        {
            /* SIGQUIT */
            case SIGTERM:
            case SIGQUIT:
                pthread_mutex_lock(&amp;mutexSignal);
                handledSignal = SIGQUIT;
                pthread_mutex_unlock(&amp;mutexSignal);
                break;

            /* SIGINT */
            case SIGINT:
                pthread_mutex_lock(&amp;mutexSignal);
                handledSignal = SIGINT;
                pthread_mutex_unlock(&amp;mutexSignal);
                break;

            /* other signals */
            default:
                pthread_mutex_lock(&amp;mutexSignal);
                handledSignal = 0;
                pthread_mutex_unlock(&amp;mutexSignal);
                break;
        }
    }

    return (void*)0;
}

/**
 * Main
 *
 */
int main()
{
    int rc, i;
    int running = 1;
    size_t stacksize;
    sigset_t signalSet;
    pthread_t threadSignalHandler;
    pthread_t thread[NUM_THREADS];
    pthread_attr_t attr;

    /* Initialise the signal mutex lock */
    pthread_mutex_init(&amp;mutexSignal, NULL);

    /* Initialise and set thread attributes */
    pthread_attr_init(&amp;attr);
    //pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_JOINABLE);
    pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_DETACHED);

    /* Ensure each thread has a given stack size - portability */
    pthread_attr_getstacksize(&amp;attr, &amp;stacksize);
    printf(&quot;Default stack size = %li\n&quot;, (long) stacksize);

        /* Determine and set a new stack size */
        stacksize = sizeof(sigset_t) + THREAD_STACK_HEADROOM;
        printf(&quot;Amount of stack calculated per thread = %li\n&quot;, (long) stacksize);
        pthread_attr_setstacksize (&amp;attr, stacksize);

    /* block all signals */
    sigfillset(&amp;signalSet);
    pthread_sigmask(SIG_BLOCK, &amp;signalSet, NULL );

    /* create the signal handling thread */
    pthread_create(&amp;threadSignalHandler, NULL, signalHandler, NULL);

    while (running &gt; 0)
    {
        /* Find an empty slot */
        for (i=0; i&lt;NUM_THREADS;i++)
        {
            if (slots[i] == 0)  /* Thread is currently not doing anything, so let's give it something to do */
            {
                /* Set this slot as used, -1 is a temporary value before being replaced by the PID of the forked process (prevents race hazards) */
                slots[i] = -1;

                /* This slot is spare */
                printf(&quot;Main: creating thread %d\n&quot;, i);

                /* Create a new thread */
                rc = pthread_create(&amp;thread[i], &amp;attr, task, (void *)i);

                if (rc)
                {
                    printf(&quot;ERROR: return code from pthread_create() is %d\n&quot;, rc);
                    exit(1);
                }

                break;
            }
            else if (slots[i] &lt; -1)     /* Thread is sleeping, see if we should wake it up */
            {
                if (time(0) &gt; (-1 * slots[i]) )
                {
                    /* Thread has slept long enough so let's wake it up */
                    slots[i] = 0;
                }
            }
        }

        pthread_mutex_lock(&amp;mutexSignal);

        switch ( handledSignal )
        {
            case -1:
                break;

            case 0:
                /* The case for signals we're not interested in */
                handledSignal = -1;
                break;

            case SIGTERM:
            case SIGQUIT:
                printf(&quot;Main: SIGQUIT\n&quot;);
                handledSignal = -1;
                running = -1;
                killall();
                break;

            case SIGINT:
                printf(&quot;Main: SIGINT\n&quot; );
                handledSignal = -1;
                running = -1;
                break;
        }

        pthread_mutex_unlock(&amp;mutexSignal);

        /* Sleep for a bit - all this thread creation is hard work! */
        usleep(100000);
    }

    waitForThreads();

    printf(&quot;Bye\n&quot;);
}
</pre>
<p>And here&#8217;s a sample PHP script (but you can of course have the wrapper call anything).</p>
<pre class="brush: php;">
&lt;?php

list($junk, $tid) = explode(&quot;=&quot;, $argv[1], 2);

printf(&quot;PHP: Hello from thread %d\n&quot;, $tid);

sleep(mt_rand(4,14));

// Non-zero exit status if the calling thread should sleep
// Chosen 250 because it's not a reserved status
// NOTE: PHP returns 255 on fatal error
exit(250);
</pre>
<h4>Exercises left to the reader</h4>
<p>The above code is not really intended as production-ready, (although I have actually used it in production) and there are still plenty of loose ends that could do with tidying up.   One thing you might want to do is detach the child PHP processes from the parent process&#8217; output sockets, thereby effectively sending them to the background.   For inspiration take a look at this basic daemonising program: <a href="http://www.4pmp.com/2009/12/a-simple-daemon-in-c/">A simple daemon in C</a>.</p>
<p>Another nice addition might be to allow the child process to be specified in command line arguments to the wrapper.   It would be useful to specify the sleep time for threads and also the maximum number of concurrent threads &#8211; this way you wouldn&#8217;t have to recompile each time you changed something.</p>
<p>If you find this useful or interesting then comments would be greatly welcomed!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.4pmp.com/2010/03/multitasking-php-in-parallel/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Spinning command line cursor in Java and PHP</title>
		<link>http://www.4pmp.com/2010/01/spinning-command-line-cursor-in-java-and-php/</link>
		<comments>http://www.4pmp.com/2010/01/spinning-command-line-cursor-in-java-and-php/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 09:46:03 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[cursor]]></category>
		<category><![CDATA[spin]]></category>
		<category><![CDATA[spinning]]></category>

		<guid isPermaLink="false">http://www.4pmp.com/?p=231</guid>
		<description><![CDATA[Update: I have created an updated article which describes a multithreaded approach in Java Spinning command line cursor in Java I think I must be really bored this morning, I can&#8217;t believe I&#8217;m actually blogging this, but it might be useful for someone, who knows.   Anyway, I am currently writing a program that sits and [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Update:</strong>   I have created an updated article which describes a multithreaded approach in Java <a href="http://www.4pmp.com/2010/01/spinning-command-line-cursor-in-java/">Spinning command line cursor in Java</a></em></p>
<p>I think I must be really bored this morning, I can&#8217;t believe I&#8217;m actually blogging this, but it might be useful for someone, who knows.   Anyway, I am currently writing a program that sits and does stuff for a very long time, and I need a way to nicely indicate the program is still running and doing its stuff.   So I have created a little method that prints a spinning cursor on the command line.   The implementation would of course have to be multithreaded; one thread to do stuff, the other to spin the cursor &#8211; if there&#8217;s time I&#8217;ll update it to a more complete solution, but for now here&#8217;s the spin implementation:</p>
<pre class="brush: java;">

public class Spin
{
    public static void main(String[] args) throws InterruptedException
    {
        String[] phases = {&quot;|&quot;, &quot;/&quot;, &quot;-&quot;, &quot;\\&quot;};

        System.out.printf(&quot;Spinning... |&quot;);

        while (true)
        {
            for (String phase : phases)
            {
                System.out.printf(&quot;\b&quot;+phase);
                Thread.sleep(100);
            }
        }
    }
}
</pre>
<p>And here it is in PHP.</p>
<pre class="brush: php;">
&lt;?php

    $phases = array(&quot;|&quot;, &quot;/&quot;, &quot;-&quot;, &quot;\\&quot;);

    printf(&quot;Spinning... |&quot;);

    while (1)
    {
        foreach ($phases AS $phase)
        {
            printf('%s%s', chr(8), $phase);
            usleep(100000); // Replace this with one iteration of doing stuff
        }
    }
</pre>
<p>Of course PHP doesn&#8217;t support threading so you&#8217;d have to call each iteration of the &#8220;doing stuff&#8221; loop inside the spin loop which would mean you&#8217;d get a bit of a jumpy spinning cursor but I think most people could live with that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.4pmp.com/2010/01/spinning-command-line-cursor-in-java-and-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Transparent aggregation in PHP5</title>
		<link>http://www.4pmp.com/2009/10/transparent-aggregation-in-php/</link>
		<comments>http://www.4pmp.com/2009/10/transparent-aggregation-in-php/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 17:17:51 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[Object oriented programming]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[acts_as]]></category>
		<category><![CDATA[aggregation]]></category>
		<category><![CDATA[magic]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[strategy]]></category>
		<category><![CDATA[transparent]]></category>

		<guid isPermaLink="false">http://www.4pmp.com/?p=88</guid>
		<description><![CDATA[The other day a Ruby developer was extolling the virtues of Ruby on Rails to me, in particular the use of &#8220;acts_as_&#8221; to make a class magically include all the behaviour from another. It isn&#8217;t inheritance, the relationship isn&#8217;t &#8220;is a&#8221; but rather &#8220;has a&#8221; &#8211; it&#8217;s a sort of transparent aggregation with all the [...]]]></description>
			<content:encoded><![CDATA[<p>The other day a Ruby developer was extolling the virtues of Ruby on Rails to me, in particular the use of &#8220;acts_as_&#8221; to make a class magically include all the behaviour from another.   It isn&#8217;t inheritance, the relationship isn&#8217;t &#8220;is a&#8221; but rather &#8220;has a&#8221; &#8211; it&#8217;s a sort of transparent aggregation with all the methods and properties of the aggregated object magically available through the containing object.   At least that&#8217;s what it looks like, from my somewhat limited RoR experience.</p>
<p>Personally, I would rather achieve this functionality by using the <a title="Strategy Pattern - Wikipedia" href="http://en.wikipedia.org/wiki/Strategy_pattern">Strategy Pattern</a> &#8211; it requires more boiler-plate code but I think the end result is much clearer.   If you&#8217;re reading this via <a href="http://www.o2.co.uk/"><strong>o2 mobile broadband</strong></a> or another portable connection to the web, then prepare to miss your stop on the train, as my temptations to re-work the functionality issues began to take over just lower down the page.</p>
<p>Anyway, not to be outdone, I set about implementing RoR&#8217;s acts_as in PHP.   My implementation works by using a class called Aggregatable which allows all child classes to aggregate, or &#8220;act_as&#8221;, other standard classes.   I&#8217;ll start with an example with two simple classes; Binman and Postman which each do different jobs.</p>
<p>Here are the classes:</p>
<pre class="brush: php;">
class Binman
{
    public function collectRubbish()
    {
        printf(&quot;Collecting rubbish\n&quot;);
    }
}
</pre>
<pre class="brush: php;">
class Postman
{
    public function deliverLetter()
    {
        printf(&quot;Delivering a letter\n&quot;);
    }
}
</pre>
<p>We can use the classes like this:</p>
<pre class="brush: php;">
$binman = new Binman();
$binman-&gt;collectRubbish();    // Prints &quot;Collecting rubbish&quot;

$postman = new Postman();
$postman-&gt;deliverLetter();    // Prints &quot;Delivering a letter&quot;
</pre>
<p>So far so good, but what if I wanted a CasualWorker class to be able to both collect rubbish and deliver letters?   I could of course copy the methods from the Binman class and the Postman class into the CasualWorker class, but this would be rather inflexible &#8211; suppose if we needed to change the Binman behaviour we would have to update both the Binman and CasualWorker classes.   Instead we make the CasualWorker class a child of the Aggregatable class and aggregate the Binman and Postman classes.   That might sound a bit complex so I&#8217;ll illustrate with an example.</p>
<p>Here&#8217;s the CasualWorker class:</p>
<pre class="brush: php;">
class CasualWorker extends Aggregatable
{
    public function __construct()
    {
        $this-&gt;aggregate(&quot;Binman&quot;);
        $this-&gt;aggregate(&quot;Postman&quot;);
    }
}
</pre>
<p>The CasualWorker class can now do everything that the Binman and Postman classes can:</p>
<pre class="brush: php;">
$casualWorker = new CasualWorker();
$casualWorker-&gt;collectRubbish();    // Prints &quot;Collecting rubbish&quot;
$casualWorker-&gt;deliverLetter();    // Prints &quot;Delivering a letter&quot;
</pre>
<h4>Great!   It works!   But how?</h4>
<p>The Aggregatable class contains an array of instances of all the classes it has been asked to aggregate.   It also makes use of <a title="Magic Methods" href="http://www.php.net/oop5.magic">PHP&#8217;s magic method</a>, __call(), so that when client code invokes a method that is not defined by it or the child class it is extending, it loops through the objects it is aggregating to see if any of those define the method.   If an object is found that defines the requested method then it is run and the result returned &#8211; just as if the invoked class defined the method.</p>
<pre class="brush: php;">
class Aggregatable
{
    /**
     * Store of aggregated objects
     *
     */
    private $aggregated = array();

    /**
     * Aggregates objects
     *
     * @param string Classname
     */
    protected function aggregate($class)
    {
        // Check an instance of this class has not already been aggregated
        if (array_key_exists($class, $this-&gt;aggregated))
        {
            throw new Exception(sprintf(&quot;Class already aggregated: %s&quot;, $class));
        }

        // Add a new instance of the class to the store
        $this-&gt;aggregated[$class] = new $class();
    }

    /**
     * Magic method - catch calls to undefined methods
     *
     * @param String method name
     * @param array Arguments
     */
    public function __call($method, $arguments)
    {
        // Loop through the aggregated objects
        foreach ($this-&gt;aggregated as $subject)
        {
            // Check each object to see if it defines the method
            if (method_exists($subject, (string) $method))
            {
                // Object defines the requested method, so call it and return the result
                return call_user_func_array(array($subject, (string) $method), $arguments);
            }
        }

        throw new Exception(sprintf(&quot;Method not found: %s&quot;, $method));
    }
}
</pre>
<h4>What about properties?</h4>
<p>I&#8217;ve kept the example above fairly simple but there&#8217;s no reason why it couldn&#8217;t be extended to cover properties using the other Magic Functions __set(), __get() and __unset().   In fact, in the example in the download (see below) I&#8217;ve implemented basic support for properties.</p>
<h4>Conclusion</h4>
<p>Like I&#8217;ve said before, I&#8217;m not a big fan of this sort of &#8220;magic&#8221;.   It seems to me to break the golden rule of object oriented programmed, as mentioned in the Gang Of Four book; &#8220;program to an interface, not an implementation&#8221;.   The interface the class provides is defined by the public properties and methods available, or even better, from the Interfaces it implements.   By adding this sort of run-time dynamism, the client code can never be sure what the class can do, <em>instanceof</em> won&#8217;t help so you&#8217;re left with either making extra functions like <em>bool isAggregating(classname)</em> or reflection.   Admittedly magic is nice, and even if a <em>isAggregating() </em>method were to solve all the problems, I would strongly advise to stay away from such temptations.</p>
<p>To quote Matt Zandstra,</p>
<blockquote><p>Magic is nice but clarity is much nicer.</p></blockquote>
<h5>Downloads</h5>
<p><a href='http://www.4pmp.com/wp-content/uploads/2009/10/aggregation.zip'>aggregation.zip</a><br />
Here&#8217;s a zip file containing all the classes mentioned above plus support for properties.   It&#8217;s perhaps worth mentioning that this really isn&#8217;t production-ready, should you ever really want to use such a thing it will need a few tweaks first &#8211; namely how to handle adding two classes which define methods of the same name &#8211; which one has priority?   No doubt you&#8217;ll also want some inspection methods such as <em>isAggregating()</em> as previously mentioned.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.4pmp.com/2009/10/transparent-aggregation-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PEAR Cache_Lite &#8211; efficient group cleaning</title>
		<link>http://www.4pmp.com/2009/09/pear-cache_lite-efficient-group-cleaning/</link>
		<comments>http://www.4pmp.com/2009/09/pear-cache_lite-efficient-group-cleaning/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 16:23:22 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[cache_lite]]></category>
		<category><![CDATA[clean]]></category>
		<category><![CDATA[disc]]></category>
		<category><![CDATA[disk]]></category>
		<category><![CDATA[efficient]]></category>
		<category><![CDATA[expire]]></category>
		<category><![CDATA[groups]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[pear]]></category>

		<guid isPermaLink="false">http://www.4pmp.com/?p=76</guid>
		<description><![CDATA[After using PEAR Cache_Lite for a while, we began to notice that as traffic increased the web servers spent more and more time thrashing their discs. On closer inspection we noticed that the servers were pretty much constantly parsing the entire cache directory structure. Whenever you call Cache_Lite::clean() to remove a group of cached elements, [...]]]></description>
			<content:encoded><![CDATA[<p>After using PEAR Cache_Lite for a while, we began to notice that as traffic increased the web servers spent more and more time thrashing their discs.   On closer inspection we noticed that the servers were pretty much constantly parsing the entire cache directory structure.</p>
<p>Whenever you call Cache_Lite::clean() to remove a group of cached elements, it parses the entire cache directory structure looking for cache files which have the correct group hash in the filename.   This was problematic for us because we stored a lot of data in groups, for example with messages &#8211; each page of messages for each user was stored in one group.   Whenever someone sends a message, the system then deletes the cache group containing the recipient&#8217;s messages.   As the cache directory structure increased in size, it took longer and longer to parse, and with increasing traffic the web servers were soon doing nothing but parsing the cache directory.</p>
<p>The solution I came up with was to prepend the name of each group with a number which was also cached.   So when a request arrives for a cached item in group &#8220;messages&#8221;, the cache system looked for the cached group identifier number and prepended it to the group name, resulting in an internal group name like &#8220;1234_messages&#8221;.</p>
<p>The overhead is an extra cache &#8220;get&#8221;, but the advantage is that in order to expire a whole group you just have to increment the identifier number by one, (get, increment, save).   So when the group is accessed again, the internal group name becomes &#8220;1235_messages&#8221;, which is not yet set, and so the application can regenerate the cache.</p>
<p>In my opinion this additional &#8220;get&#8221; is a price worth paying, especially as it&#8217;s a relatively very quick operation, and the time saved expiring a group is many times faster.</p>
<h4>Finally</h4>
<p>You might be thinking to yourself, &#8220;what about all those expired cache files just left on the disc?&#8221;.   Well, we set a CRON job to run every day and delete all files older than 3 days.   As none of the caches lasted longer than three days this was a safe duration.</p>
<p>In fact, we don&#8217;t use disc caching anywhere near as much as we did, now we use Memcached for most things, but for small and often used caches (such as IDs) we still use the disc cache as it&#8217;s by far the fastest.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.4pmp.com/2009/09/pear-cache_lite-efficient-group-cleaning/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PEAR Cache_Lite &#8211; preventing stampeding</title>
		<link>http://www.4pmp.com/2009/09/pear-cache_lite-preventing-stampeding/</link>
		<comments>http://www.4pmp.com/2009/09/pear-cache_lite-preventing-stampeding/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 20:18:13 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[cache_lite]]></category>
		<category><![CDATA[improving]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[stampeding]]></category>
		<category><![CDATA[timeout]]></category>
		<category><![CDATA[touch]]></category>

		<guid isPermaLink="false">http://www.4pmp.com/?p=35</guid>
		<description><![CDATA[The PEAR Cache_Lite package is an excellent caching system; lightweight and fast, however when put into use on a high-traffic website a few issues came to light. The first problem we hit was stampeding. What&#8217;s stampeding? Stampeding is the situation when a request, let&#8217;s say from User1, arrives for a cached item that has expired. [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://pear.php.net/package/Cache_Lite/">PEAR Cache_Lite</a> package is an excellent caching system; lightweight and fast, however when put into use on a high-traffic website a few issues came to light.   The first problem we hit was stampeding.   </p>
<h4>What&#8217;s stampeding?</h4>
<p>Stampeding is the situation when a request, let&#8217;s say from <em>User1</em>, arrives for a cached item that has expired.   The cache system returns boolean false and the process of rebuilding that cached data begins, calling the database, formatting the data and so on.   </p>
<p>If, during this process of rebuilding the cached data, another request arrives for the same cached item, let&#8217;s say from <em>User2</em>, another process of rebuilding the cached data begins.   This is because the process started by <em>User1</em> has not yet finished and so the cache system still returns boolean false when requested for the cached item.</p>
<p>So now we have two processes running, regenerating the same cache item.   The situation can get out of hand if more and more requests for the same cache item arrive &#8211; causing the load on the web server or database to increase, and everything to potentially grind to a halt.</p>
<h4>The solution</h4>
<p>What&#8217;s required is for the cache system to know that a particular cache is being regenerated and therefore return the old cache until the new data has been regenerated.   Thankfully this can be achieved very simply with the addition of just one extra line of code into the Cache_Lite class, Lite.php.</p>
<p>The trick is to <a href="http://www.php.net/touch">touch()</a> the cache file immediately after realising it has expired in the <em>Cache_Lite::get()</em> function.   After touching the file, the get function will return false and the calling code will regenerate the cache data.   </p>
<p><code>@touch($this->_file);</code></p>
<p>By touching the file, the modification time of the cache file is set to the current time and therefore all subsequent requests will think the cache is still valid and return the old data.   Once the first process has regenerated the data, it saves it and the cache file once again contains up-to-date data.</p>
<h4>Some finishing touches</h4>
<p>By touching the cache, processes immediately following the one which is regenerating the fresh data will return out-of-date data, albeit by a matter of seconds &#8211; which in most cases really won&#8217;t matter nor be noticed.</p>
<p>If, however, something were to happen to the process regenerating the data, such as an uncaught exception, database timeout, etc that it would fail and not save the cache, then the old cache will be valid until it expires again &#8211; so it will have effectively been valid for twice its intended lifetime.   </p>
<p>We can limit this by setting the modification time in the touch command to be the current time, minus the cache lifetime, plus 60s &#8211; which would mean that if the regenerating process were to fail, the cache would only be valid for another 60s.</p>
<p><code>@touch($this->_file, time() - abs($this->_lifeTime) + 60);</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.4pmp.com/2009/09/pear-cache_lite-preventing-stampeding/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

