我的功能如何持续检查收到的消息?收到消息后,以下功能退出。考虑到,队列已启用长轮询,如何持续检查新消息?
import tweepy
from tweepy import OAuthHandler
import time
consumer_key = 'x
consumer_secret = 'y'
access_token = 'a'
access_secret = 'b'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
users = tweepy.Cursor(api.followers, screen_name='realmadrid', count=200).items()
while True:
try:
user = next(users)
except tweepy.TweepError:
time.sleep(60 * 15)
user = next(users)
except StopIteration:
break
with open('BJP_Followers3.txt', 'a') as f:
f.write(user.screen_name)
f.write('\n')
print(user.screen_name)
答案 0 :(得分:3)
您的功能需要不断轮询Amazon SQS。
如果没有可用消息,长轮询会将响应延迟最多20秒。如果在此期间有消息可用,则会立即返回。如果20秒后没有消息,则返回时不提供消息。
因此,你的函数需要再次轮询SQS(或许在此期间做其他事情)。
答案 1 :(得分:2)
var processMessages = (function (err, data) {
if (data.Messages) {
for (i = 0; i < data.Messages.length; i++) {
var message = data.Messages[i];
var body = JSON.parse(message.Body);
// process message
// delete if successful
}
}
});
while (true) {
sqs.receiveMessage({
QueueUrl: sqsQueueUrl,
MaxNumberOfMessages: 5, // how many messages to retrieve in a batch
VisibilityTimeout: 60, // how long until these messages are available to another consumer
WaitTimeSeconds: 15 // how many seconds to wait for messages before continuing
}, processMessages);
}
答案 2 :(得分:0)
(function checkMessage(){
var params = {
QueueUrl : Constant.QUEUE_URL,
VisibilityTimeout: 0,
WaitTimeSeconds: 0
}
sqs.receiveMessage(params,(err,data) => {
if(data){
console.log("%o",data);
}
checkMessage()
});
})()
要连续检查aws sqs中是否有传入消息,您将希望在返回数据时重新调用aws sqs。