Package pyworker :: Module jsonworker
[hide private]
[frames] | no frames]

Source Code for Module pyworker.jsonworker

 1  #!/usr/bin/python 
 2  # -*- coding: utf-8 -*- 
 3   
 4  # Most workers are expected to use JSON msgs 
 5  import simplejson 
 6   
 7  from pyworker import STATUSES, Worker, WorkerResponse, WorkerException, JsonMsgParseError 
 8   
9 -class JSONWorker(Worker):
10 """Similar in practice to the normal Worker, except it only tolerates JSON 11 messages and will ignore any it cannot parse. 12 Passing it an outbound queue will allow it to pass any unparsable msgs onwards."""
13 - def run(self):
14 while (True): 15 # Blocking call: 16 if self.stop: 17 break 18 msg = self.queue_stdin.get() 19 # TODO implement variable timeout on .starttask() method 20 try: 21 jmsg = simplejson.loads(msg) 22 resp = self.starttask(jmsg) 23 self.endtask(jmsg, resp) 24 except Exception, e: 25 print "Failed to parse\n%s" % msg 26 print e 27 if self.queue_stdout: 28 self.queue_stdout.put(msg) 29 # Actively consume bad messages 30 self.queue_stdin.task_done()
31