I want to accept multiple concurrent request for Flask API. API is currently getting "company name" through POST
method and call the crawler engine, and each crawling process takes 5-10 minutes to finish. I want to run many crawler engine in parallel for different respective that many request. I followed this, but could not get it working. Currently, second request is cancelling the first request. How can I achieve this parallelism?
Current API implementation:
app.py
app = Flask(__name__)
app.debug = True
@app.route("/api/v1/crawl", methods=['POST'])
def crawl_end_point():
if not request.is_json:
abort(415)
inputs = CompanyNameSchema(request)
if not inputs.validate():
return jsonify(success=False, errros=inputs.errors)
data = request.get_json()
company_name = data.get("company_name")
print(company_name)
if company_name is not None:
search = SeedListGenerator(company_name)
search.start_crawler()
scrap = RunAllScrapper(company_name)
scrap.start_all()
subprocess.call(['/bin/bash', '-i', '-c', 'myconda;scrapy crawl company_profiler;'])
return 'Data Pushed successfully to Solr Index!', 201
if __name__ == "__main__":
app.run(host="10.250.36.52", use_reloader=True, threaded=True)
gunicorn.sh
#!/bin/bash
NAME="Crawler-API"
FLASKDIR=/root/Public/company_profiler
SOCKFILE=/root/Public/company_profiler/sock
LOG=./logs/gunicorn/gunicorn.log
PID=./guincorn.pid
user=root
GROUP=root
NUM_WORKERS=10 # generally in the 2-4 x $(NUM_CORES)+1 range
TIMEOUT=1200
#preload_apps = False
# The maximum number of requests a worker will process before restarting.
MAX_REQUESTS=0
echo "Starting $NAME"
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your gunicorn
exec gunicorn app:app -b 0.0.0.0:5000 \
--name $NAME \
--worker-class gevent \
--workers 5 \
--keep-alive 900 \
--graceful-timeout 1200 \
--worker-connections 5 \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--log-level info \
--backlog 0 \
--pid=$PID \
--access-logformat='%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s' \
--error-logfile $LOG \
--log-file=-
Thanks in advance!