linnarsson-lab / loom-viewer

Tool for sharing, browsing and visualizing single-cell data stored in the Loom file format
BSD 2-Clause "Simplified" License
35 stars 6 forks source link

Flask fixes with multiple connections #83

Closed JobLeonard closed 7 years ago

JobLeonard commented 7 years ago

@slinnarsson, this seems relevant to our Flask problems:

http://stackoverflow.com/questions/39806848/how-to-stop-persistent-connections-in-flask

Flask becomes unresponsive when two or more different client send requests at the same time. I tested other possibilities very extensively and can quite confidently say that the problem is being caused due to persistent connections. If I close the one of the two connections Flask immediately becomes responsive again and other request is served.

Which links to the following SO thread with two replies that might give some hints for how to fix our issues: http://stackoverflow.com/questions/12591760/flask-broken-pipe-with-requests

Run your flask app under a proper WSGI server capable of handling concurrent requests (perhaps gunicorn or uWSGI) and it'll work. While developing, enable threads in the Flask-supplied server with:

app.run(threaded=True)

JobLeonard commented 7 years ago

I just tried what would happen if we turn on threading in Flask by replacing:

app.run(debug=debug, host="0.0.0.0", port=port)

with

app.run(threaded=True, debug=debug, host="0.0.0.0", port=port)

... in this line of loom_server.py. On my computer this already greatly increased the speed of the website! Heatmap tiles load notably faster, and when testing fetching many genes - see #84 - the performance is better (but still terrible, so fetching-multiple-rows-at-once is still the way to go).

Can you check if this works fine for you too, @slinnarsson? If so we can turn it on for now, and then later we can find a proper replacement for Werkzeug before going public.

JobLeonard commented 7 years ago

In trying to make sense of what WSGI is I came across this article:

https://www.fullstackpython.com/wsgi-servers.html image image

Flask has some documentation to deploy with uWSGI, and uWSGI documentation has a "Python quickstart" page too:

http://flask.pocoo.org/docs/0.11/deploying/uwsgi/

https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html

Here is also a step-by-step first-app tutorial:

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-14-04

JobLeonard commented 7 years ago

Ok so uWSGI seems to be one of those "can-fine-tune-everything assumes-you-already-know-what-you're-doing" solutions for websites serving thousands of users where they hire a trained server person. It's overkill and quite frankly, after reading the documentation I don't get it because I lack the technical background that it assumes I already have.

In fact, just about everything out there assumes too much about what we're doing. After trying to get a whole bunch of frameworks to work, in the end I just needed to adapt the following six lines and we're done...

from gevent.wsgi import WSGIServer
from yourapplication import app

http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()

http://flask.pocoo.org/docs/0.12/deploying/wsgi-standalone/#gevent

from gevent import monkey
monkey.patch_all()

http://www.gevent.org/gevent.monkey.html

See this commit

I just deployed to loom.linnarssonlab.org (with production build of the website; I'll look into automating that next) and tried to crash it by opening 50 tabs of the biggest dataset. I froze my browser, but not the server, so I'm calling this "good enough for our expected level of traffic"

slinnarsson commented 7 years ago

👍

-- Sten Linnarsson, PhD Professor of Molecular Systems Biology Karolinska Institutet Unit of Molecular Neurobiology Department of Medical Biochemistry and Biophysics Scheeles väg 1, 171 77 Stockholm, Sweden<x-apple-data-detectors://1/0> +46 8 52 48 75 77<tel:+46%208%2052%2048%2075%2077> (office) +46 70 399 32 06<tel:+46%2070%20399%2032%2006> (mobile)

25 jan. 2017 kl. 14:30 skrev Job van der Zwan notifications@github.com<mailto:notifications@github.com>:

Closed #83https://github.com/linnarsson-lab/Loom/issues/83.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/linnarsson-lab/Loom/issues/83#event-936129372, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AKKag8Fi6ailnI53stGGI5Oq6wpALH5nks5rV05GgaJpZM4Lm4sI.

JobLeonard commented 7 years ago

Bad news: http://learn-gevent-socketio.readthedocs.io/en/latest/general_concepts.html#general-concepts-concurrency-parallelism-threads-and-processes

TL;DR: we're multithreaded, but still single-core and locked into the GIL. It shouldn't affect us too much, except when computationally heavy code hits the server. Tile generation is one example. The simple solution is to generate all tiles at file creation, of course. I won't re-open for now, but it's something to keep in mind.

mohit3wadhwa commented 4 years ago

HI, I am having the same issue but I am unable to follow this article. It seems diverted from actual issue. Can you please let me know what exactly you did to fix this issue?

Thanks, Mohit Wadhwa

naveeen684 commented 4 years ago

I can't do multiple login in my flask app? Please anybody have solution