Thursday, December 27, 2007

Speed up your Facebook Django app

One of the things I loved about the iSeries was the easy, easy way we could toss a job to batch from the middle of an interactive session.

It is also one of the things I miss while coding in Django.

What's this have to do with Facebook apps?

Every call to the FB API costs time - from 280ms to 475ms on average - and the more calls, the more time. Where submitting some work really comes in handy is during processing after a user-gesture. Want to publish a bunch of news after the user clicks "BITE ME" and not have the user sit there and wait (and maybe experience an FB timeout) while you send minifeed news and notifications to 4,219 20 of their BFFs?

Use a thread. Really, use a threadpool, but I haven't coded mine yet. It doesn't take long to code, but the holidays grabbed me around the ankle and forced me to take time with friends, so there you go.

But the basics are here:

import threading

def sbmthrjob(cmd, *args):
class newThread(threading.Thread):
def __init__(self, cmd, *args):
self.cmd = cmd
self.args = args
threading.Thread.__init__(self)

def run(self):
self.cmd(*args)

try:
thread1 = newThread(cmd, *args)
thread1.setDaemon(True)
thread1.start()

except Exception, e:
print 'sbmjob exception: ' , e
elsewhere:

sbmthrjob(cmd, argle, bargle)



What I want to do is to have the thread come from a pool of already-created threads - I'll get to that soon.

2 comments:

  1. just curious, have you looked at using a queueing system to do the work, something like beanstalkd or the like?

    useful if you do not need a direct response from facebook to the UI, but just need workers to spawn off a bunch of writes to facebook (or a db or whatever)

    ReplyDelete
  2. I never got around to coding the threadpool, which would have implemented a Queue object.

    Otherwise, what this does is pretty much what you describe.

    ReplyDelete