Using memcache to get results from a gearman background job

So we would like to do a long-running-task separately from processing a web request. Gearman is a good tool to do that. Create a background task, and done! However, what should we do when we want the results of the task? Background tasks only return a job handle. Completion notifications (what we would get if we submitted a normal, non-background task) require the task requester to stay connected to the gearman daemon, which is a little hard when the task requester is written in PHP, since the PHP script disconnects after serving the page to the user. (Strictly speaking, we can avoid this by using long-lived connections ala comet, but that's not very simple, is it?)

We solve this by using memcached to store the task's results after it completes. The task result is keyed by the job handle, which should be good enough. The monitor script can then poll the gearman daemon for the job's completion using the job handle, and if the job is complete, it can then fetch the results from memcached. Sample implementation given below:

Of course, this skimps on the security bit, since the handle is clearly visible and can be altered by the user. This is probably fine in an internal app, but it would be better put in the session, or encoded somehow. Progress reports (in case numerator / denominator isn't enough) can also be given via memcached, just agree on a key.