lvwerra HF staff commited on
Commit
fea1872
·
verified ·
1 Parent(s): b180f1a

Update jupyter/jupyter_server.py

Browse files
Files changed (1) hide show
  1. jupyter/jupyter_server.py +22 -5
jupyter/jupyter_server.py CHANGED
@@ -1,4 +1,5 @@
1
  from flask import Flask, request, jsonify
 
2
  import uuid
3
  import time
4
  import docker
@@ -9,6 +10,7 @@ import argparse
9
  import logging
10
  from pydantic import BaseModel, Field, ValidationError
11
 
 
12
 
13
  app = Flask(__name__)
14
  app.logger.setLevel(logging.INFO)
@@ -44,7 +46,7 @@ def create_kernel_containers(n_instances, n_cpus=2, mem="2g", execution_timeout=
44
 
45
  docker_client = docker.from_env()
46
  app.logger.info("Buidling docker image...")
47
- image, logs = docker_client.images.build(path='./', tag='jupyter-kernel:latest')
48
  app.logger.info("Building docker image complete.")
49
 
50
  containers = []
@@ -139,18 +141,33 @@ def execute_code():
139
  }), 500
140
 
141
 
142
- atexit.register(shutdown_cleanup)
 
 
 
 
 
 
 
 
143
 
144
- if __name__ == '__main__':
145
- args = parse_args()
146
  app.containers = create_kernel_containers(
147
  args.n_instances,
148
  n_cpus=args.n_cpus,
149
  mem=args.mem,
150
  execution_timeout=args.execution_timeout
151
- )
 
 
 
 
 
 
 
152
  # don't use debug=True --> it will run main twice and thus start double the containers
153
  app.run(debug=False, host='0.0.0.0', port=args.port)
 
 
154
 
155
 
156
  # TODO:
 
1
  from flask import Flask, request, jsonify
2
+ import os
3
  import uuid
4
  import time
5
  import docker
 
10
  import logging
11
  from pydantic import BaseModel, Field, ValidationError
12
 
13
+ current_dir = os.path.dirname(os.path.abspath(__file__))
14
 
15
  app = Flask(__name__)
16
  app.logger.setLevel(logging.INFO)
 
46
 
47
  docker_client = docker.from_env()
48
  app.logger.info("Buidling docker image...")
49
+ image, logs = docker_client.images.build(path=current_dir, tag='jupyter-kernel:latest')
50
  app.logger.info("Building docker image complete.")
51
 
52
  containers = []
 
141
  }), 500
142
 
143
 
144
+ def init_app(app, args=None):
145
+ if args is None:
146
+ # When run through Gunicorn, use environment variables
147
+ args = argparse.Namespace(
148
+ n_instances=int(os.getenv('N_INSTANCES', 1)),
149
+ n_cpus=int(os.getenv('N_CPUS', 1)),
150
+ mem=os.getenv('MEM', '1g'),
151
+ execution_timeout=int(os.getenv('EXECUTION_TIMEOUT', 60))
152
+ )
153
 
 
 
154
  app.containers = create_kernel_containers(
155
  args.n_instances,
156
  n_cpus=args.n_cpus,
157
  mem=args.mem,
158
  execution_timeout=args.execution_timeout
159
+ )
160
+ return app, args
161
+
162
+ atexit.register(shutdown_cleanup)
163
+
164
+ if __name__ == '__main__':
165
+ args = parse_args()
166
+ app, args = init_app(app, args=args)
167
  # don't use debug=True --> it will run main twice and thus start double the containers
168
  app.run(debug=False, host='0.0.0.0', port=args.port)
169
+ else:
170
+ app, args = init_app(app)
171
 
172
 
173
  # TODO: