本文整理汇总了Python中pika.BasicProperties方法的典型用法代码示例。如果您正苦于以下问题:Python pika.BasicProperties方法的具体用法?Python pika.BasicProperties怎么用?Python pika.BasicProperties使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pika
的用法示例。
在下文中一共展示了pika.BasicProperties方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: got_config_request
# 需要导入模块: import pika [as 别名]
# 或者: from pika import BasicProperties [as 别名]
def got_config_request(self, ch, method, properties, body):
ip_addresses = json.loads(body)
logging.info("Got config request with following IP addresses: %s" % ip_addresses)
pi_id = None
worker = db.session.query(db.objects.Worker).filter(db.objects.Worker.address.in_(ip_addresses)).first()
if worker:
pi_id = worker.id
logging.debug("Found worker id %s for IP address %s" % (pi_id, worker.address))
else: # wasn't able to find worker with given ip address(es)
logging.error("Wasn't able to find worker for given IP adress(es)")
reply_properties = pika.BasicProperties(correlation_id=properties.correlation_id)
self.channel.basic_publish(exchange=utils.EXCHANGE, properties=reply_properties, routing_key=properties.reply_to, body="")
return
config = self.prepare_config(pi_id)
logging.info("Sending intial config to worker with id %s" % pi_id)
reply_properties = pika.BasicProperties(correlation_id=properties.correlation_id, content_type='application/json')
self.channel.basic_publish(exchange=utils.EXCHANGE, properties=reply_properties, routing_key=properties.reply_to, body=json.dumps(config))
# callback method for when the manager recieves data after a worker executed its actions
示例2: send_json_msg
# 需要导入模块: import pika [as 别名]
# 或者: from pika import BasicProperties [as 别名]
def send_json_msg(self, rk, body, **kwargs):
if self.connection.is_open:
try:
logging.debug("Sending message to manager")
properties = pika.BasicProperties(content_type='application/json')
self.channel.basic_publish(exchange=utils.EXCHANGE, routing_key=rk, body=json.dumps(body), properties=properties, **kwargs)
return True
except Exception as e:
logging.exception("Error while sending data to queue:\n%s" % e)
return False
else:
logging.error("Can't send message to manager")
message = {"rk":rk, "body": body, "kwargs": kwargs, "json": True}
if message not in self.message_queue: # could happen if we have another disconnect when we try to clear the message queue
self.message_queue.append(message)
logging.info("Added message to message queue")
else:
logging.debug("Message already in queue")
return False
# Try to resend the messages which couldn't be sent before
示例3: message_worker
# 需要导入模块: import pika [as 别名]
# 或者: from pika import BasicProperties [as 别名]
def message_worker(self):
while 1:
try:
record, routing_key = self.queue.get()
if not self.connection or self.connection.is_closed or not self.channel or self.channel.is_closed:
self.open_connection()
self.channel.basic_publish(
exchange=self.exchange,
routing_key=routing_key,
body=record,
properties=pika.BasicProperties(
delivery_mode=2,
headers=self.message_headers
)
)
except Exception:
self.channel, self.connection = None, None
self.handleError(record)
finally:
self.queue.task_done()
if self.close_after_emit:
self.close_connection()
示例4: _make_amqp_properties_and_routing_keys
# 需要导入模块: import pika [as 别名]
# 或者: from pika import BasicProperties [as 别名]
def _make_amqp_properties_and_routing_keys(self):
input_properties = pika.BasicProperties(**{
'message_id': self.MESSAGE_ID,
'type': 'rather-not-used-by-parsers :-)',
'timestamp': self.MESSAGE_TIMESTAMP,
'headers': dict(
**self.MESSAGE_EXTRA_HEADERS),
})
if issubclass(self.PARSER_CLASS, BlackListParser):
assert not issubclass(self.PARSER_CLASS, AggregatedEventParser)
event_type = 'bl'
elif issubclass(self.PARSER_CLASS, AggregatedEventParser):
event_type = 'hifreq'
else:
event_type = 'event'
input_rk = self.PARSER_SOURCE
if self.PARSER_RAW_FORMAT_VERSION_TAG is not sentinel.not_set:
input_rk = input_rk + '.' + self.PARSER_RAW_FORMAT_VERSION_TAG
expected_output_rk = (event_type + '.parsed.' + self.PARSER_SOURCE)
return input_properties, input_rk, expected_output_rk
示例5: register_consumer
# 需要导入模块: import pika [as 别名]
# 或者: from pika import BasicProperties [as 别名]
def register_consumer(self, queue_name: str, consumer: Consumer) -> None:
def wrapped_consumer(ch: BlockingChannel,
method: Basic.Deliver,
properties: pika.BasicProperties,
body: str) -> None:
try:
consumer(ch, method, properties, body)
ch.basic_ack(delivery_tag=method.delivery_tag)
except Exception as e:
ch.basic_nack(delivery_tag=method.delivery_tag)
raise e
self.consumers[queue_name].add(wrapped_consumer)
self.ensure_queue(queue_name,
lambda: self.channel.basic_consume(queue_name, wrapped_consumer,
consumer_tag=self._generate_ctag(queue_name)))
示例6: publish_messages
# 需要导入模块: import pika [as 别名]
# 或者: from pika import BasicProperties [as 别名]
def publish_messages():
global connection, channel, published, count, published, curr_pos, last_acked, curr_del_tag, exit_triggered
for iteration in xrange(curr_pos, count):
if channel.is_open:
curr_del_tag += 1
channel.basic_publish('', queue,
'Hello World!',
pika.BasicProperties(content_type='text/plain',
delivery_mode=2))
published += 1
curr_pos += 1
# don't let the publishing advance further than 10000 messages past acks
if curr_del_tag - last_acked > 9999:
if channel.is_open:
connection.add_timeout(5, publish_messages)
break
else:
print("Channel closed, ceasing publishing")
break
if curr_pos == count and exit_triggered == False:
exit_triggered = True
exit_program()
示例7: properties
# 需要导入模块: import pika [as 别名]
# 或者: from pika import BasicProperties [as 别名]
def properties(self) -> dict:
"""
Returns a dict from which a :class:`pika.BasicProperties` object can be created
In this implementation, the following is returned:
- headers
- content message_type
- priority
- message id
- message message_type
"""
headers = {self.type_header: self.message.task}
content_type = self.content_type
priority = self.message.priority
message_id = str(self.message.uuid)
message_type = self.message_type
return dict(headers=headers, content_type=content_type, priority=priority, message_id=message_id,
type=message_type)
示例8: poll_mutation_queue
# 需要导入模块: import pika [as 别名]
# 或者: from pika import BasicProperties [as 别名]
def poll_mutation_queue(self):
"""
In this paradigm calling means pushing our message
to the queue (the callback will take care of it)
and wait for the response and process it.
@returns: string, serialized MutationObject (only attributes)
"""
self.response = None
self.corr_id = str(uuid.uuid4())
self.channel.basic_publish(exchange = '', # default exchange
routing_key = 'rpc_mutations_queue',
properties = pika.BasicProperties(
reply_to = self.callback_queue,
correlation_id = self.corr_id),
body = 'POLL MUTATION QUEUE')
self.ae.m_info("[x] Sent mutation queue poll")
while self.response is None:
# Waiting for a response
self.connection.process_data_events()
return self.response
示例9: send_evaluation
# 需要导入模块: import pika [as 别名]
# 或者: from pika import BasicProperties [as 别名]
def send_evaluation(self, mutation_object):
"""
In this paradigm calling means pushing our message
to the queue (the callback will take care of it)
and wait for the response and process it.
@returns: string, serialized MutationObject (only attributes)
"""
self.response = None
self.corr_id = str(uuid.uuid4())
self.channel.basic_publish(exchange = '', # default exchange
routing_key = 'rpc_evaluations_queue',
properties = pika.BasicProperties(
reply_to = self.callback_queue,
correlation_id = self.corr_id),
# This should be a serialized
# evaluation object
body = mutation_object.serialize_me())
self.ae.m_info("[x] Sent evaluation")
while self.response is None:
# Waiting for a response
self.connection.process_data_events()
return self.response
示例10: on_mutation_request
# 需要导入模块: import pika [as 别名]
# 或者: from pika import BasicProperties [as 别名]
def on_mutation_request(self, ch, method, props, body):
"""Callback for messages in the 'rpc_mutations_queue'
They say: "Hey, do you have a mutation for me?"
"""
# This is the "remote procedure"
# being called and returning a value
mutation_obj = self.get_mutation()
ch.basic_publish(exchange = '',
routing_key = props.reply_to,
properties = pika.BasicProperties(
correlation_id = props.correlation_id),
body = mutation_obj.serialize_me())
ch.basic_ack(delivery_tag = method.delivery_tag)
示例11: publish_message
# 需要导入模块: import pika [as 别名]
# 或者: from pika import BasicProperties [as 别名]
def publish_message(self, body, durable=True):
self.__channel.exchange_declare(exchange=self.__exchange, exchange_type=self.__exchange_type)
if self.__queue_name:
result = self.__channel.queue_declare(queue=self.__queue_name)
else:
result = self.__channel.queue_declare(exclusive=True)
self.__channel.queue_bind(exchange=self.__exchange, queue=result.method.queue)
if durable:
properties = pika.BasicProperties(delivery_mode=2)
self.__channel.basic_publish(exchange=self.__exchange, routing_key=self.__routing_key, body=body,
properties=properties)
else:
self.__channel.basic_publish(exchange=self.__exchange, routing_key=self.__routing_key, body=body)
ins_log.read_log('info', 'Publish message %s sucessfuled.' % body)
示例12: call
# 需要导入模块: import pika [as 别名]
# 或者: from pika import BasicProperties [as 别名]
def call(self, parameters):
assert type(parameters) == str
self.id = str(uuid.uuid4())
properties = pika.BasicProperties(reply_to=self.callback_queue, correlation_id=self.id)
self.channel.basic_publish(
exchange='', routing_key=self.rabbit_queue, properties=properties, body=parameters
)
while self.response is None:
time.sleep(3)
print(" [*] Got fitness for individual {}".format(json.loads(parameters)[0]))
self.responses.put(self.response)
# Job is completed, remove job order from queue
self.jobs.get()
self.jobs.task_done()
# Close RabbitMQ connection to prevent file descriptors limit from blocking server
self.connection.close()
示例13: on_request
# 需要导入模块: import pika [as 别名]
# 或者: from pika import BasicProperties [as 别名]
def on_request(self, channel, method, properties, body):
i, genes, additional_parameters = json.loads(body)
# If an additional parameter is received as a list, convert to tuple
for param in additional_parameters.keys():
if isinstance(additional_parameters[param], list):
additional_parameters[param] = tuple(additional_parameters[param])
print(" [.] Evaluating individual {}".format(i))
# print(" ... Genes: {}".format(str(genes)))
# print(" ... Other: {}".format(str(additional_parameters)))
# Run model and return fitness metric
individual = self.individual(self.x_train, self.y_train, genes=genes, **additional_parameters)
fitness = individual.get_fitness()
# Prepare response for master and send it
response = json.dumps([i, fitness])
channel.basic_publish(
exchange='', routing_key=properties.reply_to,
properties=pika.BasicProperties(correlation_id=properties.correlation_id), body=response
)
channel.basic_ack(delivery_tag=method.delivery_tag)
示例14: send
# 需要导入模块: import pika [as 别名]
# 或者: from pika import BasicProperties [as 别名]
def send(message_json):
"""
:param message:
:return:
"""
global logger
connection = pika.BlockingConnection(pika.ConnectionParameters(rabbit_host))
channel = connection.channel()
queue = channel.queue_declare(queue=queue_name, durable=True, auto_delete=True)
logger.debug("send> number of messages in the queue is: "+str(queue.method.message_count))
message = json.dumps(message_json)
logger.debug("send> sending message")
logger.debug(message)
channel.basic_publish(exchange='',
routing_key=queue_name,
body=message,
properties=pika.BasicProperties(
delivery_mode=2, # make message persistent
))
connection.close()
示例15: sendData
# 需要导入模块: import pika [as 别名]
# 或者: from pika import BasicProperties [as 别名]
def sendData(self, server, queue, ci, data):
self.logger.debug('Sending [{}] to [{}] Start'.format(ci, server))
## conn = pika.BlockingConnection(
## pika.ConnectionParameters(server)
## )
with pika.BlockingConnection(pika.ConnectionParameters(host = server,
port = self.MQ_PORT)) as conn:
for line in data:
chan = conn.channel()
chan.queue_declare(queue = queue, durable = True)
chan.basic_publish(exchange = '', routing_key = queue, body = line,
properties = pika.BasicProperties(delivery_mode = 2, ))
self.logger.debug('Sending [{}] to [{}] Done'.format(ci, server))
return(True)
## run func