I am sending a post request to function which is coroutine but I am getting below error.
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1994, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1615, in full_dispatch_request
return self.finalize_request(rv)
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1630, in finalize_request
response = self.make_response(rv)
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1740, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/usr/local/lib/python3.4/site-packages/werkzeug/wrappers.py", line 885, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "/usr/local/lib/python3.4/site-packages/werkzeug/wrappers.py", line 57, in _run_wsgi_app
return _run_wsgi_app(*args)
File "/usr/local/lib/python3.4/site-packages/werkzeug/test.py", line 884, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'generator' object is not callable
Below is function which call after submit button: and function f() is fetching some data from a get request and return the json from response.
@app.route('/submit', methods=['POST'])
@asyncio.coroutine
def submit():
result = yield from f()
return render_template('test.html', result=result)
def f()
response = yield from request('get',...)
....
You can't use coroutines as Flask routes, that's not how Flask, or the WSGI protocol, work. WSGI handles one request/response per thread/process/eventlet. If you need to do background tasks, spawn a thread or use a task queue like Celery.
You can return a response containing a generator to stream the response. It will still run synchronously in one thread though.
If you use gevent, you can use eventlets instead of threads to handle each request, in which case you can spawn other eventlets. This comes with it's own performance issues, since all eventlets run in a loop in one thread (this is true of asyncio too). Gunicorn also supports aiohttp, but Flask does not, although it may be possible to get it to work.