Sending Django’s WSGIRequest to a celery task

There may end up being an instance or 2 where you need access to the initial request object inside a celery task, for want of forming an absolute URL for an email template that you’re generating asynchronously, amongst many other reasons. However you’ll find that the WSGIRequest object can’t be pickled because of bound methods and lambda’s that exist on the object itself. So when you try to send the task, it’ll fall over raising an Exception stating that the WSGIRequest object can’t be pickled.

No worries! Here we monkey patch WSGIRequest’s __reduce__ method to allow it to be pickled safely. You’ll need to drop this into a file that gets imported relatively early on in the project. What I usually do is have a monkey.py file sitting at the root of my project, and in the settings file simply “import monkey”.

METACOPY = ['HTTP_HOST', 'REMOTE_ADDR', 'HTTPS', 'SERVER_NAME', 'SERVER_PORT']
def __reduce__(self):
    from authentication.models import AnonymousUser
    from django.http import HttpRequest
 
    request_dict = {
        'META': dict([
            (k, self.META[k]) for k in METACOPY
            if k in self.META and
            isinstance(self.META[k], (int, str, unicode, bool))
        ]),
        'POST': self.POST,
        'GET': self.GET,
        'user': self.user if hasattr(self, 'user') else AnonymousUser(),
        'path': self.path
    }
    return HttpRequest, (), request_dict
from django.core.handlers.wsgi import WSGIRequest
WSGIRequest.__reduce__ = __reduce__

Basically all we’re doing here is creating a new dictionary that only includes the most amount of information that the WSGIRequest object needs to reconstruct itself with. The bound methods that prevent it from being pickled in the first place can be recreated when the pickled object is expanded (and for the most part you’ll probably find you don’t use them anyway). All we really care about is the META, GET, POST, user and the path info, all of which we’re including here. This makes the WSGIRequest object pickle-able, and allows access to a proper request object within a celery task.

I’ve been using this code in production for little over a year now with zero side effects, so I can vouch for its safety.

Happy pickling!

Dell Inspiron 5558 Review

I’ve never done a hardware review before (with the exception of the odd Amazon review), so bear with me.

For those who’ve been following my rarely updated blog, I’ve been plagued with laptop issues for a while (here, here, here and here – oddly enough all LCD related) so I figured it was about time I found some sort of reliable work horse that I can just *get stuff done* rather than having to faff around with hardware every handful of months (the last post about LVDS cables covers a significant period of time, and recently started playing up again). So it was about time for a change.

Continue reading “Dell Inspiron 5558 Review” »

Setting up WordPress behind Amazon Cloudfront

You all know that feeling (well, I hope you do!) that when a spike in traffic occurs on your WordPress site, that the miniature server you have it running on very quickly runs out of resources. Apache’s good like that. Taking up all the resources with its large number of processes consuming oodles of memory each. How on earth can you possibly fix it, especially when you’re running on a tight budget and upgrading the server for that once-in-a-blue-moon you get a spike in traffic? Well, Amazon Cloudfront to the rescue!

Continue reading “Setting up WordPress behind Amazon Cloudfront” »

A lesson on LVDS, impedance and cheap cables from China

A handful of months ago, my laptop (Lenovo Ideapad Y500 for those wondering) started suffering with screen issues. Specifically the ones that result from the tiny wires in the LCD cables shearing in the hinges. Taking it upon myself to go to eBay to find a replacement, I found a single seller selling the part for the grand total of $15, all the way from some hole in China. Estimated delivery 3-4 weeks. *sigh*.

Continue reading “A lesson on LVDS, impedance and cheap cables from China” »

Making WordPress’s qTranslate plugin work on different domain names

Recently I was handed a WordPress site for a client that used qTranslate for various versions of their website in different languages. The caveat was that each language needed its own domain name, not a subdomain, path or query string of the main one. For sure I could have redirected the welsh domain to the english version with the /cy/ path in it, but I had a better idea. Use nginx. This is utterly genius.

Continue reading “Making WordPress’s qTranslate plugin work on different domain names” »