import sys
import time
import pika
import json

class PipeRouter(object):
    class NoConnection(Exception): pass

    def __init__(self, amqp_host):
        self.amqp_host = amqp_host

    def hand_off_json(self, queue, args):
        return self.hand_off(queue, json.dumps(args))

    def hand_off(self, queue, body):
        connection = pika.BlockingConnection(
            pika.ConnectionParameters(host=self.amqp_host))
        channel = connection.channel()
        channel.queue_declare(queue=queue, durable=True)
        channel.basic_publish(exchange='',
                              routing_key=queue,
                              body=body,
                              properties=pika.BasicProperties(delivery_mode=2))
        connection.close()

    def get_connection(self, host):
        count = 0.4
        while count < 60:
            try:            
                connection = pika.BlockingConnection(
                    pika.ConnectionParameters(host=host))
                return connection
            except:
                time.sleep(count)
                count *= 1.7
        raise NoConnection

    def handle_queue(self, queue_name, callback_fn):
        connection = self.get_connection(self.amqp_host)
        try:
            channel = connection.channel()
            channel.queue_declare(queue=queue_name, durable=True)
            channel.basic_qos(prefetch_count=1)
            channel.basic_consume(callback_fn, queue=queue_name)
            channel.start_consuming()
        except:
            pass
        finally:
            connection.close()

    def handle_queue_forever(self, queue_name, callback_fn):
        while True:
            self.handle_queue(queue_name, callback_fn)

    def create_handler(self, f, errors_queue):
        def callback(ch, method, properties, body):
            try:
                results = f(body)
            except:
                error_msg = {
                    "error": {
                        "type": sys.exc_info()[0],
                        "err_str": sys.exc_info()[1],
                        "orig_body": body
                        }
                    }
                results = [ (errors_queue, error_msg) ]
            try:
                for r in results:
                    queue, msg = r
                    self.hand_off_json(queue, msg)
            finally:
                ch.basic_ack(delivery_tag = method.delivery_tag)
        return callback

    def route(self, callback=None, input_queue=None, error_queue=None):
        cb = self.create_handler(callback, error_queue)
        self.handle_queue_forever(input_queue, cb)

@@ -179,16 +179,77 @@
 p_host%0A%0A
+    def stop(self):%0A        self.connection.close()%0A        %0A
     def 
@@ -391,38 +391,34 @@
 onnection = 
-pika.BlockingC
+self.get_c
 onnection(%0A 
@@ -407,32 +407,33 @@
 .get_connection(
+)
 %0A            pik
@@ -429,60 +429,17 @@
     
-    pika.ConnectionParameters(host=self.amqp_host))%0A
+try:%0A    
     
@@ -473,32 +473,36 @@
 annel()%0A        
+  
+  
 channel.queue_de
@@ -526,32 +526,36 @@
 , durable=True)%0A
+    
         channel.
@@ -611,16 +611,20 @@
         
+    
 routing_
@@ -664,16 +664,20 @@
         
+  
+  
 body=bod
@@ -679,16 +679,20 @@
 y=body,%0A
+    
         
@@ -763,16 +763,37 @@
 ode=2))%0A
+        finally:%0A    
         
@@ -843,14 +843,8 @@
 self
-, host
 ):%0A 
@@ -1022,16 +1022,26 @@
 rs(host=
+self.amqp_
 host))%0A 
@@ -1278,30 +1278,16 @@
 nection(
-self.amqp_host
 )%0A      
@@ -1582,19 +1582,35 @@
        p
-ass
+rint sys.exc_info()
 %0A       
@@ -2051,16 +2051,20 @@
 %22type%22: 
+str(
 sys.exc_
@@ -2072,16 +2072,17 @@
 nfo()%5B0%5D
+)
 ,%0A      
@@ -2110,16 +2110,20 @@
 r_str%22: 
+str(
 sys.exc_
@@ -2131,16 +2131,17 @@
 nfo()%5B1%5D
+)
 ,%0A      
@@ -2280,16 +2280,77 @@
 _msg) %5D%0A
+            ch.basic_ack(delivery_tag = method.delivery_tag)%0A
         
@@ -2519,56 +2519,12 @@
     
-ch.basic_ack(delivery_tag = method.delivery_tag)
+pass
 %0A   

fix error handling; factor initialisation together