0

I have a flask restful api with an endpoint

api.add_resource(TestGet, '/api/1/test')

and I want to use the data from that endpoint to populate my jinja template. But everytime I try to call it in a sample route like this

@app.route('/mytest') def mytest(): t = get('http://localhost:5000/api/1/test') It never returns anything and stays in a loop meaning it is doing something with the request and never returns. Is there a reason I am not able to call it within the same flask app? I am able to reach the endpoint on the browser and from another python REPL. Thoroughly confused why this would happen and why it never returns anything. At least expecting an error.

Here is the entire sample of what I am trying to run

from flask import Flask
from requests import get

app = Flask('test')

from flask_restful import Api, Resource

api = Api(app)

class TestGet(Resource):
    def get(self):
        return {'test': 'message'}

api.add_resource(TestGet, '/test')

@app.route('/something')
def something():
    resp = get('http://localhost:5000//test').json
    print(resp)
from gevent.wsgi import WSGIServer
WSGIServer(('', 5000), app).serve_forever()
michael
  • 113
  • 9

2 Answers2

0

Please see this SO thread with nice explanation of Flask limitations: https://stackoverflow.com/a/20862119/5167302 Specifically, in your case you are hitting this one:

The main issue you would probably run into is that the server is single-threaded. This means that it will handle each request one at a time, serially. This means that if you are trying to serve more than one request (including favicons, static items like images, CSS and Javascript files, etc.) the requests will take longer. If any given requests happens to take a long time (say, 20 seconds) then your entire application is unresponsive for that time (20 seconds).

Hence by making request from within request you are putting your application into deadlock.

Timofey Chernousov
  • 1,284
  • 8
  • 12
  • Thats what I was thinking too! So I added a WSGI server using gevent. Simple 1 line `WSGIServer(('', 5000), app).serve_forever()` and i thought this would help. But I still think there is that issue. – michael Oct 23 '17 at 04:14
0

Use app.run(threaded=True) if you just want to debug your program. This will start a new thread for every request.

zstbsqx
  • 60
  • 8
  • Cool. This was a quick fix to the problem. Any thoughts on what @timofey mentioned about flask limitations? This maybe out of scope but now adding a server that is not just single threaded will hopefully help. I want to use something like gunicorn but on a windows so decided gevent for development. – michael Oct 23 '17 at 04:21
  • I usually use gunicorn with gevent worker on linux. Maybe uwsgi will work on windows? Besides, if you're using win10, I would recommend its ubuntu subsystem. – zstbsqx Oct 23 '17 at 08:19
  • It's odd. Do you know of the config to make gevent to work similarly? I have to config above but never got it to work. Just the debug server works. – michael Oct 23 '17 at 23:13
  • If you're going to use gevent wsgi in code, it's necessary to [monkey patch](http://www.gevent.org/gevent.monkey.html) your code because gevent can't handle blocking built-in functions. I run gunicorn in command with supervisor like this: `gunicorn -k gevent_wsgi -b 0.0.0.0:8000 -w 4 myapp:app`, which will do monkey patching automatically. – zstbsqx Oct 24 '17 at 04:14