I'm trying to run a flask application which polls an AWS SQS queue, I require the application to continuously poll and respond to client requests asynchronously, however I find that application to be blocked while it's polling.
I've read other posts about using gevent, but I can't seem to get it working.
Command used to run the application,
gunicorn src.app:app \
--bind 0.0.0.0:8081 \
--timeout 127 \
-k gevent --worker-connections=2000
The application code,
from flask import Flask
from werkzeug.middleware.proxy_fix import ProxyFix
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from src import security
from src.config import config
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = config.pg_url
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
migrate = Migrate(app, db)
app.wsgi_app = ProxyFix(app.wsgi_app)
from src.app import routes
from src.app.services.sqs.queue import SQSQueue
from src.app.services.sqs.message_processor import MessageProcessor
if config.group == 'JobServer':
SQSQueue.poll(MessageProcessor)
Code for polling,
def poll(action):
while True:
try:
message = self.get_message()
if message is None:
time.sleep(5)
else:
# do something
except Exception:
pass
Using the gevent worker is not enough. You will also need to use it in your code. For your code, patching the IO libraries will likely be enough.
To keep things simple I will typically use monkey.patch_all()
as early as possible in my imports.
For example in your applications import I would place it at the top like so
# Use gevent to patch IO modules as soon as possible.
from gevent import monkey; monkey.patch_all()
from flask import Flask
from werkzeug.middleware.proxy_fix import ProxyFix
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from src import security
from src.config import config
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.