forked from plotly/dash-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dash-maximum-number-of-requests.py
72 lines (56 loc) · 2.02 KB
/
dash-maximum-number-of-requests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import dash
import dash_core_components as dcc
import dash_html_components as html
import flask
import gevent
import os
import pandas as pd
from psycogreen.gevent import patch_psycopg
import sqlalchemy
import sqlalchemy_gevent
import time
server = flask.Flask(__name__)
app = dash.Dash(__name__, server=server)
server.secret_key = os.environ.get('secret_key', 'secret')
engine = sqlalchemy.create_engine(
'{dialect}://{username}:{password}@{host}:{port}/{schema}'.format(
dialect='postgres',
username='masteruser',
password='connecttoplotly',
host='readonly-test-postgres.cwwxgcilxwxw.us-west-2.rds.amazonaws.com',
port=5432,
schema='plotly_datasets'
)
)
engine.pool._use_threadlocal = True
app.layout = html.Div([
html.Button('Add a CPU bound request', id='cpu-click', n_clicks=0),
html.Output(id='cpu-container'),
html.Button('Add a IO bound request', id='io-click', n_clicks=0),
html.Output(id='io-container'),
html.Button('gevent sleep', id='gevent-sleep-click', n_clicks=0),
html.Output(id='gevent-sleep-container'),
])
@app.callback(
dash.dependencies.Output('cpu-container', 'children'),
[dash.dependencies.Input('cpu-click', 'n_clicks')])
def process(n_clicks):
print("Processing cpu request #{}".format(n_clicks))
time.sleep(10)
return 'Done (request #{})'.format(n_clicks)
@app.callback(
dash.dependencies.Output('io-container', 'children'),
[dash.dependencies.Input('io-click', 'n_clicks')])
def process(n_clicks):
print("Processing io request #{}".format(n_clicks))
pd.read_sql('SELECT pg_sleep(10)', engine)
return 'Done (request #{})'.format(n_clicks)
@app.callback(
dash.dependencies.Output('gevent-sleep-container', 'children'),
[dash.dependencies.Input('gevent-sleep-click', 'n_clicks')])
def process(n_clicks):
print("Processing gevent sleep request #{}".format(n_clicks))
gevent.sleep(10)
return 'Done (request #{})'.format(n_clicks)
if __name__ == '__main__':
app.run_server(debug=True)