在 Amazon 中处理消息

Processing Messages in Amazon

这可能是一个愚蠢的问题,但我现在有很大的心理障碍。我看不到我的 SQS 队列和将处理消息的 EC2 实例之间的连接。

也就是说:客户端在网页上完成一个表单(这个网页托管在一个EC2实例中),然后那个表单作为消息发送到一个SQS队列。在那之后,我的云应用程序的目标是采用消息的形式信息和 运行 a .sh 与该信息。此过程的示例如下图所示:

那么,我的 SQS 队列 运行 如何在 EC2 实例中生成 .sh?我想出用 Python-Boto 这样做的唯一方法是创建一个 "listener" 来不断阅读消息,并 "do something" 使用该消息。

while 1:
    m = conn.receive_message(
        q, 
        number_messages=1, 
        message_attributes=['configName'], 
        attributes='All'
    )
if not m:
    time.sleep(5)
else:
    a = str(m[0].message_attributes.get('configName').get('string_value'))
    rh = str(m[0].receipt_handle)
    # Processing the message in a EC2 instance
    conn.delete_message_from_handle(q, rh)
    time.sleep(5)

所以,如上面的代码所示,我读取了消息的属性(在本例中为简单起见,只有一个)并且我必须处理它。

如何在不同的 EC2 实例中并行处理传入消息?我不知道怎么做,因为我只有一个侦听器,如果侦听器正忙于处理一条消息,则在完成第一条消息之前不会处理任何其他消息。我希望侦听器并行处理任意数量的消息(当然,这取决于实例数量和我要支付的账单)。我如何知道哪个 EC2 实例将 运行 我的 .sh 程序?

还有另一种方法可以通过任何亚马逊服务以更简单的方式执行此操作吗?

谢谢

So, how can my SQS Queue run a .sh in a EC2 instance?

您似乎已经用自己的代码回答了这个问题。

The only way I figured out to do so with Python-Boto, is creating a "listener" to constantly read the messages and "do something" with that message.

是的,队列就是这样工作的。

How can I process the incoming messages in parallel in different EC2 instances?

只是 运行 处理多个 EC2 实例上的消息的代码

I don't see how, because I only have one listener, and if the listener is busy processing one message, it won't process any other until it finishes the first one.

这对你的单线程来说是正确的。因此,您可以创建多个线程 运行ning 您拥有的循环。您还可以创建多个进程,其中包含这些线程(在同一个 VM 或多个 VM 上)。

How can I know which EC2 instance will run my .sh program?

你不知道。无论哪个 EC2 实例选择它,都会 运行 它。这通常是通过队列分离前端和后端处理的预期行为。

There is another way to do this in a much easier way with any Amazon Service?

不,这种方法是最简单的,假设您正在尝试将 and/or 从前端 Web 服务器卸载支持处理。我觉得一点都不复杂。

另外,去掉你的睡眠,改用 long polling