Flask not executing function when wrapped in a decorator
Question:
I have an api based on flask. I am using also the multiprocessing module which may be involved in the following issue.
When using a non trivial wrapper the flask app just does not execute the wrapped task. There is also no error message und an no way to step into the task at hand with the pycharm debugger. The minimum example for such a decorator is the following
def test(task):
def g(*args,**kwargs):
return task(*args,**kwargs)
return g
but this works:
def test(task):
return task
Any Idea what is going on? The task is executed in a multiprocessing Pool
the task is executed in the following manner:
pool = multiprocessing.Pool(10, maxtasksperchild=10)
@app.route('/', methods=['POST'])
def test():
task = other_script.task
for i in range(3):
pool.apply_async(task,args=(i,))
and in this other script we have something like:
@test
def task(arg):
return 2
It would be really verbose to go into all the details how the api exactly works. so please feel free to share an educated guess based on this limited information
Answers:
try this way:
from flask import Flask, Response
from functools import wraps
app = Flask(__name__)
app.config['TESTING'] = True
def token_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
kwargs['hello'] = 'world!'
return f( *args, **kwargs)
return decorated_function
@app.route("/",methods=["GET"])
@token_required
def index(*args, **kwargs):
return kwargs['hello']
if __name__ == '__main__':
app.run(port=5000,debug=True)
I have an api based on flask. I am using also the multiprocessing module which may be involved in the following issue.
When using a non trivial wrapper the flask app just does not execute the wrapped task. There is also no error message und an no way to step into the task at hand with the pycharm debugger. The minimum example for such a decorator is the following
def test(task):
def g(*args,**kwargs):
return task(*args,**kwargs)
return g
but this works:
def test(task):
return task
Any Idea what is going on? The task is executed in a multiprocessing Pool
the task is executed in the following manner:
pool = multiprocessing.Pool(10, maxtasksperchild=10)
@app.route('/', methods=['POST'])
def test():
task = other_script.task
for i in range(3):
pool.apply_async(task,args=(i,))
and in this other script we have something like:
@test
def task(arg):
return 2
It would be really verbose to go into all the details how the api exactly works. so please feel free to share an educated guess based on this limited information
try this way:
from flask import Flask, Response
from functools import wraps
app = Flask(__name__)
app.config['TESTING'] = True
def token_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
kwargs['hello'] = 'world!'
return f( *args, **kwargs)
return decorated_function
@app.route("/",methods=["GET"])
@token_required
def index(*args, **kwargs):
return kwargs['hello']
if __name__ == '__main__':
app.run(port=5000,debug=True)