简体   繁体   中英

How to send data between 2 EC2 instances

I have two AWS EC2 instances, one running a.py and b.py. These two programs use data produced by the other to complete tasks, a.py waits for b.py to create some data that it uses to create some data that a.py will use to create data that b.py will.... basically, they will keep passing data back and forth until a condition is met.

I haven't been able to find a place that concretely defines how to do this. Optimally, I want to do this with the smallest time lag.

As you are using AWS already, the native solution for things like that is SQS queue. To achieve that task, you need to create two SQS queue:

  • SQS-Queue-App-A
  • SQS-Queue-App-B

Then make a.py, something along these lines:

import boto3

# Create SQS client
sqs = boto3.client('sqs')

queue_a_url = 'SQS_QUEUE_URL'
queue_b_url = 'SQS_QUEUE_URL'

while (1):
  messages = sqs.receive_messages(
        MaxNumberOfMessages=10,
        WaitTimeSeconds=10,
        QueueUrl=queue_a_url,
    )
  for msg in messages:
    logger.info("Received: %s: %s", msg.message_id, msg.body)
    #Do whatever you need to do with the message 
    
    response = sqs.send_message(
      QueueUrl=queue_b_url,

      MessageBody=(
        'something to process by script B'
      )
    )

You can create them as FIFO queues to be sure that messages are in sequences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM