-
azure-15 storage 3 - queue클라우드/azure 2023. 5. 6. 15:18
1. queue
메시지를 저장할 수 있는 저장소이고 queue 하나당 64kb를 저장할 수 있다.
큐를 사용하는 목적은 서비스와 서비스 사이에 메시지를 전달하여 여유를 주어서 loosely coupled achetecture를 만들기 위해서 사용된다. 서비스 구조가 너무 타이트하면 한 번에 데이터가 많이 들어올 때 등 여러 상황에서 문제가 발생할 수 있기 때문이다.
2. 큐 생성
!pip install azure-storage-queue==2.1.0 Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/ Collecting azure-storage-queue==2.1.0 Downloading azure_storage_queue-2.1.0-py2.py3-none-any.whl (24 kB) Collecting azure-storage-common~=2.1 Downloading azure_storage_common-2.1.0-py2.py3-none-any.whl (47 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 47.8/47.8 kB 3.8 MB/s eta 0:00:00 Collecting azure-common>=1.1.5 Downloading azure_common-1.1.28-py2.py3-none-any.whl (14 kB) Requirement already satisfied: python-dateutil in /usr/local/lib/python3.9/dist-packages (from azure-storage-common~=2.1->azure-storage-queue==2.1.0) (2.8.2) Requirement already satisfied: cryptography in /usr/local/lib/python3.9/dist-packages (from azure-storage-common~=2.1->azure-storage-queue==2.1.0) (40.0.2) Requirement already satisfied: requests in /usr/local/lib/python3.9/dist-packages (from azure-storage-common~=2.1->azure-storage-queue==2.1.0) (2.27.1) Requirement already satisfied: cffi>=1.12 in /usr/local/lib/python3.9/dist-packages (from cryptography->azure-storage-common~=2.1->azure-storage-queue==2.1.0) (1.15.1) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.9/dist-packages (from python-dateutil->azure-storage-common~=2.1->azure-storage-queue==2.1.0) (1.16.0) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.9/dist-packages (from requests->azure-storage-common~=2.1->azure-storage-queue==2.1.0) (2022.12.7) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.9/dist-packages (from requests->azure-storage-common~=2.1->azure-storage-queue==2.1.0) (3.4) Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.9/dist-packages (from requests->azure-storage-common~=2.1->azure-storage-queue==2.1.0) (1.26.15) Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.9/dist-packages (from requests->azure-storage-common~=2.1->azure-storage-queue==2.1.0) (2.0.12) Requirement already satisfied: pycparser in /usr/local/lib/python3.9/dist-packages (from cffi>=1.12->cryptography->azure-storage-common~=2.1->azure-storage-queue==2.1.0) (2.21) Installing collected packages: azure-common, azure-storage-common, azure-storage-queue Successfully installed azure-common-1.1.28 azure-storage-common-2.1.0 azure-storage-queue-2.1.0
큐 모듈을 불러온다.
from azure.storage.queue import QueueService, QueueMessageFormat connect_str='' queue_name = 'queue-myqueue' queue_service = QueueService(connection_string=connect_str) queue_service.create_queue(queue_name)
연결 문자열을 입력하고 queue를 생성한다.
3. 큐에 메시지 넣기
queue_service.encode_function = QueueMessageFormat.binary_base64encode# 엔코딩 queue_service.decode_function = QueueMessageFormat.binary_base64decode# 디코딩
import base64 #base64 방식 message = '-helo world3' print('Adding messge'+ message) message= base64.b64encode(message.encode('utf-8')) #유니코드 방식으로 엔코드 queue_service.put_message(queue_name, message) # 큐에 메시지 넣기 Adding messge-helo world3 <azure.storage.queue.models.QueueMessage at 0x7f4470b9e020>
큐에 메시지를 넣는다.
4. 큐 peek, get, delete
messages = queue_service.peek_messages(queue_name) for peeked_message in messages: message = base64.b64decode(peeked_message.content) # 메시지 디코딩 print('peeked message:'+message.decode('utf-8')) peeked message:-helo world3
peek로 가장 먼저 들어간 메시지를 옅볼 수 있다. 큐의 내용을 가져오지는 않는다.
message = queue_service.get_messages(queue_name) for msg in message: messagee = base64.b64decode(msg.content) print('got message'+messagee.decode('utf-8')) # utf8을 통해 디코딩
get은 큐에 들어간 메시지를 모두 꺼내온다. peek은 메시지가 여전히 남아있지만 get은 메시지를 꺼내온다는 차이점이 있다.
queue_service.delete_queue(queue_name) True
큐를 삭제한다.
'클라우드 > azure' 카테고리의 다른 글
azure-14 storage 2 - file storage 2 - python (0) 2023.05.06 azure-12 storage 1 - blob storage 2 - python (0) 2023.05.06 azure-10 sql database 4 - sql server (0) 2023.05.05 azure-09 sql database 3 - vscode, python (1) 2023.05.05