I have a webapp using Flask, Gunicorn and Nginx. In accordance with the documentation, I'm using a worker count larger than the number of CPUs.
However, when I have a scheduled task that writes to a file, it's being rewritten once by each worker, instead of being written only once overall.
the gunicorn configuration
command = /home/brun0/infraestruturas/venv/bin/gunicorn -w 3 app:app
if i use 1 instead of 3 workers it only prints once (like i want) but i dont know if its good to do this with only 1 worker
flask app
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
def trains():
try:
data = list(getRequest())
trainList = getTrain(data)
delayedSet = getDelayedCSV(trainList)
delayedCounter(delayedSet)
except Exception as e:
print(e)
scheduler = BackgroundScheduler()
scheduler.add_job(func=trains, trigger='interval', minutes=10 ,max_instances=100, misfire_grace_time=None)
scheduler.start()
if __name__ == '__main__':
app.run()