本文整理汇总了Python中azure.storage.queue.QueueService.put_message方法的典型用法代码示例。如果您正苦于以下问题:Python QueueService.put_message方法的具体用法?Python QueueService.put_message怎么用?Python QueueService.put_message使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类azure.storage.queue.QueueService
的用法示例。
在下文中一共展示了QueueService.put_message方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: AzureProvider
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import put_message [as 别名]
class AzureProvider(BaseProvider):
"""Queue Provider for the Microsoft Azure."""
def __init__(self, account_name, account_key):
self.queue_service = QueueService(
account_name=account_name, account_key=account_key
)
def put_message(self, queue_name: str, message: str):
self.queue_service.put_message(
queue_name=queue_name,
message_text=message,
)
def get_message(self, queue_name: str):
try:
queue_message = self.queue_service.get_messages(
queue_name=queue_name, numofmessages=1
).queue_messages[0]
except IndexError:
return {}
else:
return {
attr_name: getattr(queue_message, attr_name)
for attr_name in dir(queue_message)
if not attr_name.startswith('_')
}
def delete_message(self, queue_name: str, message: dict):
message_id = message.get('message_id')
pop_receipt = message.get('pop_receipt')
self.queue_service.delete_message(
queue_name, message_id=message_id, popreceipt=pop_receipt,
)
示例2: QueueStorageHandler
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import put_message [as 别名]
class QueueStorageHandler(logging.Handler):
"""
Handler class which sends log messages to a Azure Storage queue.
"""
def __init__(self,
account_name=None,
account_key=None,
protocol='https',
queue='logs',
message_ttl=None,
visibility_timeout=None,
base64_encoding=False,
is_emulated=False,
):
"""
Initialize the handler.
"""
logging.Handler.__init__(self)
self.service = QueueService(account_name=account_name,
account_key=account_key,
is_emulated=is_emulated,
protocol=protocol)
self.meta = {'hostname': gethostname(), 'process': os.getpid()}
self.queue = _formatName(queue, self.meta)
self.queue_created = False
self.message_ttl = message_ttl
self.visibility_timeout = visibility_timeout
self.base64_encoding = base64_encoding
def emit(self, record):
"""
Emit a record.
Format the record and send it to the specified queue.
"""
try:
if not self.queue_created:
self.service.create_queue(self.queue)
self.queue_created = True
record.hostname = self.meta['hostname']
msg = self._encode_text(self.format(record))
self.service.put_message(self.queue,
msg,
self.visibility_timeout,
self.message_ttl)
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
def _encode_text(self, text):
if self.base64_encoding:
text = b64encode(text.encode('utf-8')).decode('ascii')
# fallback for the breaking change in azure-storage 0.33
elif sys.version_info < (3,):
if not isinstance(text, unicode):
text = text.decode('utf-8')
return text
示例3: __init__
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import put_message [as 别名]
class TimeLineWorker:
QUEUE_KEY = ''
ACCOUNT_NAME = ''
QUEUE_NAME = ''
def __init__(self):
self.queue_service = QueueService(account_name=TimeLineWorker.ACCOUNT_NAME,
account_key=TimeLineWorker.QUEUE_KEY)
def insert_message(self):
obj = {
"message": "test message",
"other_key": 10
}
message = unicode(json.dumps(obj))
self.queue_service.put_message(TimeLineWorker.QUEUE_NAME, message)
def get_next_message(self):
messages = self.queue_service.get_messages(TimeLineWorker.QUEUE_NAME)
for message in messages:
print message.content
self.queue_service.delete_message(TimeLineWorker.QUEUE_NAME, message.id, message.pop_receipt)
示例4: test_sas_add
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import put_message [as 别名]
def test_sas_add(self):
# SAS URL is calculated from storage key, so this test runs live only
if TestMode.need_recordingfile(self.test_mode):
return
# Arrange
token = self.qs.generate_shared_access_signature(
self.test_queues[0],
self._get_shared_access_policy(QueueSharedAccessPermissions.ADD),
)
# Act
service = QueueService(
account_name=self.settings.STORAGE_ACCOUNT_NAME,
sas_token=token,
)
self._set_service_options(service, self.settings)
result = service.put_message(self.test_queues[0], 'addedmessage')
# Assert
result = self.qs.get_messages(self.test_queues[0])
self.assertEqual('addedmessage', result[0].message_text)
示例5: test_sas_add
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import put_message [as 别名]
def test_sas_add(self):
# SAS URL is calculated from storage key, so this test runs live only
if TestMode.need_recording_file(self.test_mode):
return
# Arrange
queue_name = self._create_queue()
token = self.qs.generate_queue_shared_access_signature(
queue_name,
QueuePermissions.ADD,
datetime.utcnow() + timedelta(hours=1),
)
# Act
service = QueueService(
account_name=self.settings.STORAGE_ACCOUNT_NAME,
sas_token=token,
)
self._set_service_options(service, self.settings)
result = service.put_message(queue_name, u'addedmessage')
# Assert
result = self.qs.get_messages(queue_name)
self.assertEqual(u'addedmessage', result[0].content)
示例6: __init__
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import put_message [as 别名]
class Queue:
def __init__(self, account_name, account_key, queue_name="logqueue"):
"""Initialiaze a queue. The type is set by the
'ACS_LOGGING_QUEUE_TYPE' environment variable. If it is set to
'AzureStorageQueue' then values must be provided for
'account_name' and 'account_key' which are values associated
with the Azure Storage account. 'queue_name' is optional and
defaults to 'logqueue'.
"""
self.log = Log()
self.queue_type = config.ACS_LOGGING_QUEUE_TYPE
self.queue_name = queue_name
# self.log.debug("Queue type: " + self.queue_type + " / " + self.queue_name)
if self.queue_type == "AzureStorageQueue":
self.createAzureQueues(account_name, account_key)
elif self.queue_type == "LocalFile":
self.file_queue = open(config.UNPROCESSED_LOG_FILE, "w+")
else:
self.log.error("Unknown queue type: " + queue_type)
def createAzureQueues(self, account_name, account_key):
"""
Create a queue for unprocessed log messages. Entries in the queue
will be dequeued, processed and deleted upon success.
"""
self.queue_service = QueueService(account_name, account_key)
self.queue_service.create_queue(self.queue_name)
def close(self):
"""Perform any necessary clearnup on the queue
at the end of a run.
"""
if self.queue_type == "AzureStorageQueue":
pass
elif self.queue_type == "LocalFile":
self.file_queue.close()
else:
self.log.error("Unknown queue type: " + queue_type)
def enqueue(self, msg, level="INFO"):
msg = level + " - " + msg
if self.queue_type == "LocalFile":
file_queue.write(msg + "\n")
elif self.queue_type == "AzureStorageQueue":
self.queue_service.put_message(self.queue_name, msg)
self.log.debug(msg)
def dequeue(self):
messages = []
if self.queue_type == "LocalFile":
with open(config.UNPROCESSED_LOG_FILE, "r") as f:
messages = f.readlines()[1]
elif self.queue_type == "AzureStorageQueue":
messages = self.queue_service.get_messages(self.queue_name)
return messages
def delete(self, message):
self.queue_service.delete_message(self.queue_name, message.message_id, message.pop_receipt)
# with open(config.PROCESSED_LOG_FILE, 'a') as processed:
# processed.write(log)
# os.remove(config.UNPROCESSED_LOG_FILE)
def delete_queue(self, queue_name):
self.queue_service.delete_queue(queue_name, False)
def getLength(self):
"""
Get the approximate length of the queue
"""
queue_metadata = self.queue_service.get_queue_metadata(self.queue_name)
count = queue_metadata["x-ms-approximate-messages-count"]
return count
示例7: instance
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import put_message [as 别名]
# Main thread - Check if port 25565 and move to the next if it's not. Once a free port has been found, create a new instance (up to 3)
while instanceCreated == 0:
freeport = checkPort(defaultport)
if freeport == 0:
defaultport += 1
counter += 1
if counter > maxInstances:
print "Too many instances! you can't run more than %s instances" % maxInstances
sys.exit()
else:
if debug == 1: print "Port %s is free" % defaultport
container = createMcInstance(defaultport)
instanceCreated = 1
counter +=1
# Return Instance ID, port and total number of instances once finished creating the server
print "Instance ID: %s" % container["Id"]
print "Listening on port %s" % defaultport
print "There are %s instances running on this server" % counter
# Update servers queue
queue_service = QueueService(account_name, account_key)
try:
queue_service.create_queue(queuename)
except:
print "Queue creation failed."
queue_service.put_message(queuename, '{"ContainerID":"%s", "ContainerPort":"%s", "ServerID":"%s"}' % container["Id"], defaultport, socket.gethostname())
sys.exit()
示例8: put_to_queue
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import put_message [as 别名]
def put_to_queue(name, content):
queue_service = QueueService(account_name=config.AZURE_STORAGE_NAME, account_key=config.AZURE_STORAGE_KEY)
queue_service.create_queue(name)
queue_service.put_message(name, content)
示例9: QueueService
# 需要导入模块: from azure.storage.queue import QueueService [as 别名]
# 或者: from azure.storage.queue.QueueService import put_message [as 别名]
#!/usr/bin/python
import sys
from azure.storage.queue import QueueService
account_name = "mcdockerqueue"
queuename = sys.argv[1]
account_key = sys.argv[2]
queue_service = QueueService(account_name, account_key)
try:
queue_service.create_queue(queuename)
except:
print "Queue creation failed."
for i in range(0,4):
queue_service.put_message(queuename, 'Hello World %s' %i)
print i