I'm new to python, so this might be a dumb question, but I have the following issue:
I'm trying to deploy a Flask-SocketIO app to heroku, my app.py looks like this:
app = Flask(__name__)
socketio = SocketIO(app)
opt: Dict[Any, Any] = {}
.
.
@socketio.on('connect')
def joined():
test = json.dumps(opt)
emit('test', test)
.
.
if __name__ == '__main__':
opt = setup_args()
socketio.run(app)
My procfile looks like this:
web: gunicorn -k flask_sockets.worker app:app
If i run heroku local my server starts as expected, and I can establish a socket conection with my client, but my variable opt seems not to be filled. From what i've read in the docs, this is because the procfile does the socketio.run(app) for me, and my __main__ part is not getting executed.
I need to somehow trigger a method that initializes some variables in my app.py.
How can I achieve this?
Thanks
opt = setup_args()out of the if statment, and move it, say, somewhere at the top of the file? Because this would solve the problem. If you wantoptto be used for each request, you should use objectg.globalbefore assignment? If I understand it correctly, if I assign a variable in a function scope, it's only available in that function, if I useglobal optopt = setup_args()the global variableoptis assigned. is that correct?gis available to you on every request. And your other statements seem to be correct.opt['agent'] = xyzI get the following error:NameError: name 'opt' is not definedSo that means that my variable is not initialized if I'm not mistaken