Chú ý: Đây không phải là dự định như là một câu trả lời trực tiếp cho câu hỏi. Thay vào đó là tăng thêm thành @TimothyLiu's answer, giả sử người dùng cuối đang sử dụng gói Boto
(còn gọi là Boto2) chứ không phải Boto3
. Mã này là một "Boto-2-ization" của delete_messages
gọi nêu tại his answer
Một
Boto
(2) kêu gọi
delete_message_batch(messages_to_delete)
nơi
messages_to_delete
là một đối tượng
dict
với chính: giá trị tương ứng với
id
:
receipt_handle
cặp trả
AttributeError: 'dict' object has no attribute 'id'.
Có vẻ như delete_message_batch
mong đợi đối tượng lớp Message
; sao chép các Boto source for delete_message_batch
và cho phép nó sử dụng một đối tượng không Message
(ala boto3) cũng không thành công nếu bạn đang xóa nhiều hơn 10 "thư" tại một thời điểm. Vì vậy, tôi phải sử dụng công việc sau đây.
đang ePrint của here
from __future__ import print_function
import sys
from itertools import islice
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
@static_vars(counter=0)
def take(n, iterable, reset=False):
"Return next n items of the iterable as same type"
if reset: take.counter = 0
take.counter += n
bob = islice(iterable, take.counter-n, take.counter)
if isinstance(iterable, dict): return dict(bob)
elif isinstance(iterable, list): return list(bob)
elif isinstance(iterable, tuple): return tuple(bob)
elif isinstance(iterable, set): return set(bob)
elif isinstance(iterable, file): return file(bob)
else: return bob
def delete_message_batch2(cx, queue, messages): #returns a string reflecting level of success rather than throwing an exception or True/False
"""
Deletes a list of messages from a queue in a single request.
:param cx: A boto connection object.
:param queue: The :class:`boto.sqs.queue.Queue` from which the messages will be deleted
:param messages: List of any object or structure with id and receipt_handle attributes such as :class:`boto.sqs.message.Message` objects.
"""
listof10s = []
asSuc, asErr, acS, acE = "","",0,0
res = []
it = tuple(enumerate(messages))
params = {}
tenmsg = take(10,it,True)
while len(tenmsg)>0:
listof10s.append(tenmsg)
tenmsg = take(10,it)
while len(listof10s)>0:
tenmsg = listof10s.pop()
params.clear()
for i, msg in tenmsg: #enumerate(tenmsg):
prefix = 'DeleteMessageBatchRequestEntry'
numb = (i%10)+1
p_name = '%s.%i.Id' % (prefix, numb)
params[p_name] = msg.get('id')
p_name = '%s.%i.ReceiptHandle' % (prefix, numb)
params[p_name] = msg.get('receipt_handle')
try:
go = cx.get_object('DeleteMessageBatch', params, BatchResults, queue.id, verb='POST')
(sSuc,cS),(sErr,cE) = tup_result_messages(go)
if cS:
asSuc += ","+sSuc
acS += cS
if cE:
asErr += ","+sErr
acE += cE
except cx.ResponseError:
eprint("Error in batch delete for queue {}({})\nParams ({}) list: {} ".format(queue.name, queue.id, len(params), params))
except:
eprint("Error of unknown type in batch delete for queue {}({})\nParams ({}) list: {} ".format(queue.name, queue.id, len(params), params))
return stringify_final_tup(asSuc, asErr, acS, acE, expect=len(messages)) #mdel #res
def stringify_final_tup(sSuc="", sErr="", cS=0, cE=0, expect=0):
if sSuc == "": sSuc="None"
if sErr == "": sErr="None"
if cS == expect: sSuc="All"
if cE == expect: sErr="All"
return "Up to {} messages removed [{}]\t\tMessages remaining ({}) [{}]".format(cS,sSuc,cE,sErr)
tôi có thể không thực sự làm điều đó, vì các tin nhắn trong SQS có một thời gian chờ tầm nhìn, vì vậy nếu tôi lần đầu tiên được 10 tin nhắn, sau đó lặp lại một vài lần, lần sau tôi có thể nhận được 10 tin nhắn giống nhau kể từ khi hết thời gian chờ. Tôi đang suy nghĩ về việc sử dụng 'dump()' nhưng tôi sẽ phải đọc các tập tin sau, có vẻ ngớ ngẩn, tôi thiếu cái gì? (Tôi có thể đặt chế độ hiển thị_timeout trong một thời gian rất dài, nhưng điều đó có vẻ xấu). –
@linker - bạn nói bạn cần kiểm tra các thông điệp cụ thể 'n'. điều này có nghĩa là có một số tiêu chí đối sánh mà bạn đang so sánh từng thư không? –
Xin lỗi nếu điều đó gây nhầm lẫn, tôi đã cập nhật bài đăng của mình. –