2

I am building a flask webapp that communicates with another application through http requests. So I am trying to using dependency injection for the httpClient object.
Here is the code

class HttpClient(object):

    def __init__(self, host):
        self.host = host
        self.httpclient = 3partyModule.connect(url=host, 
                                                           verbose=False, 
                                                           max_greenlets=1)

def configure(binder):

    binder.bind(HttpClient, to=HttpClient, scope=singleton)
    
    
if __name__ == '__main__':

    app = Flask(__name__)
    FlaskInjector(app=app, modules=[configure])
    app.run()

When I run the application, I get the following error -

Exception has occurred: RuntimeError
Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.

I have tried to lookup on this, but could not find any helpful lead. Appreciate if anyone can shed some light on the issue here.

Thank you in advance.

Package Versions:

Flask==2.0.1
Flask-Injector==0.12.3
Python==3.8

  • https://stackoverflow.com/a/9932189/15011621 – charchit Jul 21 '21 at 15:07
  • could you show imports in `3partyModule`? – Danila Ganchar Jul 21 '21 at 15:30
  • [here](https://github.com/triton-inference-server/client/blob/main/src/python/library/tritonclient/http/__init__.py) is the third party class "InferenceServerClient" that I use to get a connection object. `3partyModule.connect(url, verbose, max_greenlets) == InferenceServerClient(url, verbose, max_greenlets)` – Pratheek Ponnuru Jul 27 '21 at 10:36

2 Answers2

0

Check your Werkzeug version. Noticed this issue with Werkzeug==2.0.1. Had to rollback to Werkzeug==0.15.2.

pip install --upgrade Werkzeug==0.15.2
0

I had this issue with Flask-Injector==0.12.3 and managed to resolve it by upgrading to Flask-Injector==0.13.0

levsa
  • 930
  • 9
  • 16