Search code examples
pythonnginxflaskgunicornwerkzeug

Flask app takes up to 4 seconds to load because of {built-in method poll}


I'm not sure why this is happening, but my flask app is taking almost 4 seconds for the initial request to load. I've connected it to a profiler which revealed this:

PATH: '/'
         37033 function calls (34635 primitive calls) in 4.029 seconds

   Ordered by: internal time, call count
   List reduced from 1405 to 30 due to restriction <30>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        8    3.888    0.486    3.888    0.486 {built-in method poll}

This only happens on the initial request, and any subsequent requests resolve almost immediately.

I'm currently running this from manage.py with:

@manager.command
def profile():
    app.config['PROFILE'] = True
    app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])
    app.run()

But the delay is there whether I run locally using gunicorn, or in production with nginx reverse proxied to gunicorn.

I cannot for the life of me figure out what {built-in method poll} is and why it's costing 4 seconds to load my app.


Solution

  • This was the result of a poorly configured flask assets. Do not do this:

         'public/css/vendor/font-awesome.min.css',
         Bundle('public/css/app.styl',
    -           depends='/**/*.styl',
                filters='stylus',
                output='public/css/app.css'),
         filters='cssmin',
    

    - showing the stupid thing not to do.