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 so far
Leave a reply

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