Python Boto3未接收消息,但SQS在飞行中显示

2024-04-27 01:02:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个docker,它从标准SQS获取消息。但大多数情况下,代码显示它没有收到任何消息并退出。虽然SQS控制台在“飞行中的消息”下显示消息,但一些消费者收到了消息

这是我的docker入口

ENV PYTHONPATH="$PYTHONPATH:/app"

ENTRYPOINT [ "python3" ]
CMD ["multi.py"]

这是multi.py代码

import multiprocessing as mp
import subprocess

def s():
    subprocess.call(['python3', 'script.py'])

n_process = min(mp.cpu_count(), 8)
process = []

for i in range(n_process):
    p = mp.Process(target=s)
    process.append(p)
    p.start()

for p in process:
    p.join()

这是调用receive_消息的代码的script.py部分

sqs = boto3.resource('sqs', region_name=REGION, aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
queue = sqs.get_queue_by_name(QueueName=QUEUE_NAME)

def main():
    while True:
        m = queue.receive_messages()
        for message in m:
            process_message(message)
            message.delete()

此外,码头工人60%的时间都在工作。但我想弄清楚为什么失败了

PS:已解决 这是来自boto3文档的

Short poll is the default behavior where a weighted random set of machines is sampled on a ReceiveMessage call. Thus, only the messages on the sampled machines are returned. If the number of messages in the queue is extremely small, you might not receive any messages in a particular ReceiveMessage response. If this happens, repeat the request.


Tags: thedocker代码inpy消息messagefor