def run(self):
        '''Runs the pipeline step.

        '''
        queue = QueueClient(account_url=os.getenv('AZ_QS_AC_URL'),
                            queue_name=os.getenv('AZ_QS_QUEUE_NAME'),
                            credential=os.getenv('AZ_QS_SAS_TOKEN'))
        response = queue.receive_messages(messages_per_page=5)
        for batch in response.by_page():
            for message in batch:
                filename = message.content
                label, _ = classifier.predict(filename)
                self.print('\'{filename}\' classified as \'{label}\'',
                           filename=filename,
                           label=label['name'])
                with open(self.output['classifier'], 'a') as f:
                    f.write(message.content + ',' + str(label['id']) + '\n')
                if label['name'] != 'other':
                    entities = ner.predict(filename)
                    self.print('\'{filename}\' has entities \'{entities}\'',
                               filename=filename,
                               entities=entities)
                    with open(self.output['ner'], 'a') as f:
                        f.write(message.content + ',\"' + str(entities) +
                                '\"\n')
                queue.delete_message(message)
示例#2
0
def main(accountName, queueName):
    accountURL = "https://%s.queue.core.windows.net" % (accountName)
    creds = ManagedIdentityCredential()
    client = QueueClient(account_url=accountURL,
                         queue_name=queueName,
                         credential=creds)

    messages = client.receive_messages(messages_per_page=1)

    for message in messages:
        print(message.content)
        client.delete_message(message)
        def _pop_specific_q(_q: QueueClient, _n: int) -> bool:
            has_messages = False
            for m in _q.receive_messages(messages_per_page=_n):
                if m:
                    has_messages = True
                    result.append(m if raw else self._deserialize_message(m))
                    if delete:
                        _q.delete_message(m.id, m.pop_receipt)

                    # short circuit to prevent unneeded work
                    if len(result) == n:
                        return True
            return has_messages
###
# Use the Azure Storage Storage SDK for Python to read each message from the Queue
###
print(
    '\nWith some messages in our Azure Storage Queue, let\'s read the first message in the Queue to signal we start to process that customer\'s order.'
)
raw_input('Press Enter to continue...')

# When you get each message, they become hidden from other parts of the applications being able to see it.
# Once you have successfully processed the message, you then delete the message from the Queue.
# This behavior makes sure that if something goes wrong in the processing of the message, it is then dropped back in the Queue for processing in the next cycle.
messages = queue_client.receive_messages()
for message in messages:
    print('\n' + message.content)
    queue_client.delete_message(message.id, message.pop_receipt)

raw_input('\nPress Enter to continue...')
metadata = queue_client.get_queue_properties()

print(
    'If we look at the Queue again, we have one less message to show we have processed that order and a yummy pizza will be on it\'s way to the customer soon.'
)
print('Number of messages in the queue: ' +
      str(metadata.approximate_message_count))
raw_input('\nPress Enter to continue...')

###
# This was a quick demo to see Queues in action.
# Although the actual cost is minimal since we deleted all the messages from the Queue, it's good to clean up resources when you're done
###