नोट: इस सवाल के लिए एक सीधा जवाब के रूप में इरादा नहीं है। इसके बजाय यह @TimothyLiu's answer पर एक संवर्धन है, मानते हैं कि अंतिम उपयोगकर्ता Boto
पैकेज (उर्फ बोटो 2) Boto3
का उपयोग नहीं कर रहा है। इस कोड को एक delete_messages
कॉल करने के लिए भेजा के "Boto-2-ization" his answer
में है एक
Boto
(2)
delete_message_batch(messages_to_delete)
के लिए कॉल जहां
messages_to_delete
कुंजी के साथ एक
dict
वस्तु है: मान इसी
id
रहे हैं:
receipt_handle
जोड़े रिटर्न
AttributeError: 'dict' object has no attribute 'id'.
ऐसा लगता है कि delete_message_batch
Message
क्लास ऑब्जेक्ट की अपेक्षा करता है; Boto source for delete_message_batch
की प्रतिलिपि बनाने और इसे एक गैर-Message
ऑब्जेक्ट (एला boto3) का उपयोग करने की इजाजत देता है, यदि आप एक समय में 10 से अधिक "संदेश" हटा रहे हैं तो भी विफल हो जाता है। तो, मुझे निम्नलिखित कार्य-आसपास का उपयोग करना पड़ा। से 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)
मैं वास्तव में है कि, के बाद से SQS में संदेश एक दृश्यता समय समाप्ति है, इसलिए यदि मैं पहली बार नहीं कर सकते हैं 10 संदेश, फिर कुछ बार लूप करें, अगली बार जब मुझे टाइमआउट पास हो गया है तो मुझे वही 10 संदेश मिलेंगे। मैं 'डंप()' का उपयोग करने के बारे में सोच रहा हूं लेकिन मुझे फ़ाइल को पढ़ने के बाद, मुझे मूर्खतापूर्ण लगता है, क्या मुझे कुछ याद आ रहा है? (मैं दृश्यता_टाउटआउट को बहुत लंबे समय तक सेट कर सकता हूं, लेकिन यह बदसूरत लगता है)। –
@ लिंकर - आपने कहा कि आपको 'n' विशिष्ट संदेशों की जांच करने की आवश्यकता है। क्या इसका मतलब यह है कि कुछ मिलान मानदंड हैं जिनके लिए आप प्रत्येक संदेश की तुलना कर रहे हैं? –
क्षमा करें अगर वह भ्रमित था, मैंने अपनी पोस्ट अपडेट की है। –