def __init__(self, b_rabbit, executor_name: str, routing_key: str, response_listener): """ TaskRequesterSynchron requests tasks synchronously. :param str executor_name: Name of Executor :param str routing_key: Routing Key of task which is set by Executor :param callable response_listener: User response listener which is called """ assert type(executor_name ) is str, "executor_name argument must be a string" assert type( routing_key) is str, "routing_key argument must be a string" if not b_rabbit.connection: raise Exception( 'Create Instance of Class RabbitMqCommunicationInterface first' ) self.b_rabbit, self.corr_id, self.executor_name = b_rabbit, str( uuid.uuid4()), executor_name with b_rabbit.connection.channel() as channel: self.channel, self.routing_key = channel, routing_key self.exchange_name, self.response_listener = executor_name + '_tasks', response_listener self.exchange = rabbitpy.DirectExchange( channel=channel, name=self.exchange_name, durable=True) self.exchange.declare() logger.info( f'Exchange: {self.exchange_name} was successfully declared from task Requester: {executor_name} ' )
def test_create(self): name = 'direct-exchange-name' rabbitpy.create_direct_exchange(exchange_name=name) with rabbitpy.Connection() as conn: with conn.channel() as channel: obj = rabbitpy.DirectExchange(channel, name) obj.declare(True) obj.delete()
def __init__(self, b_rabbit, executor_name: str, routing_key: str, task_listener): """ TaskExecutor registers on Task which is triggered by TaskRequester. :param str executor_name: Name of Executor :param str routing_key: Routing Key of task :param callable task_listener: User task listener which is called """ assert type( executor_name) is str, "executor name should be a string" assert type(routing_key) is str, "routing key should be a string" if not b_rabbit.connection: raise Exception('Create Instance of Class BRabbit first') self.exchange_name, self.routing_key = executor_name + '_tasks', routing_key self.b_rabbit, self.task_listener = b_rabbit, task_listener self.executor_name, self.exchange_name = executor_name, self.exchange_name with b_rabbit.connection.channel() as channel: exchange = rabbitpy.DirectExchange(channel=channel, name=self.exchange_name, durable=True) exchange.declare()
import os import rabbitpy import time from ch6 import utils # Open the channel and connection connection = rabbitpy.Connection() channel = connection.channel() exchange = rabbitpy.DirectExchange(channel, 'rpc-replies') exchange.declare() # Create the response queue that will automatically delete, is not durable and # is exclusive to this publisher queue_name = 'response-queue-%s' % os.getpid() response_queue = rabbitpy.Queue(channel, queue_name, auto_delete=True, durable=False, exclusive=True) # Declare the response queue if response_queue.declare(): print('Response queue declared') # Bind the response queue if response_queue.bind('rpc-replies', queue_name): print('Response queue bound') # Iterate through the images to send RPC requests for for img_id, filename in enumerate(utils.get_images()):