The cake is a lie

Code storytelling

It’s fun writing code that tells a story. Hopefully it’s also fun reading it.


class TerminationNotice(Exception):
    "Exception raised inside a thread when it's time for it to die."
    pass
 
class SuicideJob(QueryJob):
    "A worker receiving this job will commit suicide."
    def run(self):
        raise TerminationNotice()
 
class QueryWorker(Thread):
    "Devoted worker who will pull jobs from the `jobs` queue and perform them."
    def __init__(self, jobs):
        self.jobs = jobs
        Thread.__init__(self)
 
    def run(self):
        "Get jobs from the queue and perform them."
        while 1:
            job = self.jobs.get()
            if not isinstance(job, QueryJob):
                debug.error("%r ate a job that wasn't a edible: %r" % (self, job))
                continue
            try:
                job.run()
            except TerminationNotice:
                # Nice knowing you :(
                break
1 comment

1 Comment so far

  1. NinjaTails October 5th, 2008 11:33 pm

    Ouch! Hope that one didn’t actually happen :P

Leave a reply

Preview: