WSGI application that demonstrates dirty worker integration.
(environ, start_response)
| 28 | |
| 29 | |
| 30 | def app(environ, start_response): |
| 31 | """WSGI application that demonstrates dirty worker integration.""" |
| 32 | path = environ.get('PATH_INFO', '/') |
| 33 | method = environ.get('REQUEST_METHOD', 'GET') |
| 34 | |
| 35 | # Parse query string |
| 36 | query = parse_qs(environ.get('QUERY_STRING', '')) |
| 37 | |
| 38 | # Get dirty client |
| 39 | client = get_dirty_client() |
| 40 | |
| 41 | try: |
| 42 | if path == '/': |
| 43 | result = { |
| 44 | "message": "Dirty Workers Demo", |
| 45 | "dirty_enabled": client is not None, |
| 46 | "pid": os.getpid(), |
| 47 | "endpoints": { |
| 48 | "/models": "List loaded models", |
| 49 | "/load?name=MODEL": "Load a model", |
| 50 | "/inference?model=NAME&data=INPUT": "Run inference", |
| 51 | "/unload?name=MODEL": "Unload a model", |
| 52 | "/fibonacci?n=NUMBER": "Compute fibonacci", |
| 53 | "/prime?n=NUMBER": "Check if prime", |
| 54 | "/stats": "Get dirty worker stats", |
| 55 | "/session/login?user_id=ID&name=NAME": "Login user (stash demo)", |
| 56 | "/session/get?user_id=ID": "Get session (stash demo)", |
| 57 | "/session/list": "List all sessions (stash demo)", |
| 58 | "/session/logout?user_id=ID": "Logout user (stash demo)", |
| 59 | "/session/stats": "Get stash stats (stash demo)", |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | elif path == '/models': |
| 64 | if client is None: |
| 65 | result = {"error": "Dirty workers not enabled"} |
| 66 | else: |
| 67 | result = client.execute( |
| 68 | "examples.dirty_example.dirty_app:MLApp", |
| 69 | "list_models" |
| 70 | ) |
| 71 | |
| 72 | elif path == '/load': |
| 73 | name = query.get('name', ['model1'])[0] |
| 74 | if client is None: |
| 75 | result = {"error": "Dirty workers not enabled"} |
| 76 | else: |
| 77 | result = client.execute( |
| 78 | "examples.dirty_example.dirty_app:MLApp", |
| 79 | "load_model", |
| 80 | name |
| 81 | ) |
| 82 | |
| 83 | elif path == '/inference': |
| 84 | model = query.get('model', ['default'])[0] |
| 85 | data = query.get('data', ['test input'])[0] |
| 86 | if client is None: |
| 87 | result = {"error": "Dirty workers not enabled"} |