Skip to content

Commit

Permalink
Add multiple workers.
Browse files Browse the repository at this point in the history
This uses the soft-deprecated multiprocessing mode of Tornado. Later
versions will switch away from it.

Ref: tornadoweb/tornado#2801
  • Loading branch information
willangley committed Oct 19, 2021
1 parent 2f9b3ed commit 69b85d7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ entrypoint: >-
./pdf_sprinkles_web.py
--address ''
--port $PORT
--workers 4
--cloud_debugger
--cloud_logging
--golink='go/pdf-sprinkles'
Expand Down
31 changes: 19 additions & 12 deletions pdf_sprinkles_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import google.cloud.logging
from google.cloud.logging.handlers import setup_logging
import pdf_sprinkles
import tornado.httpserver
import tornado.ioloop
import tornado.web
import trace_context
Expand All @@ -37,6 +38,7 @@
flags.DEFINE_integer('port', 8888, 'port to listen to.')
flags.DEFINE_string('address', '127.0.0.1', 'address to bind to.')
flags.DEFINE_boolean('debug', False, 'Starts Tornado in debugging mode.')
flags.DEFINE_integer('workers', 1, 'Number of workers to start.')
flags.DEFINE_string(
'golink', None, 'If set, displays a go/ link in the header.')
flags.DEFINE_boolean('cloud_debugger', False, 'Use cloud debugger.')
Expand Down Expand Up @@ -93,6 +95,22 @@ def main(argv: Sequence[str]) -> None:
if len(argv) > 1:
raise app.UsageError('Too many command-line arguments.')

application = tornado.web.Application(
[
(r'/', MainHandler),
(r'/_ah/warmup', WarmupHandler),
(r'/recognize', RecognizeHandler, None, 'recognize'),
],
static_path=os.path.join(os.path.dirname(__file__), 'static'),
template_path=os.path.join(os.path.dirname(__file__), 'templates'),
debug=FLAGS.debug,
autoreload=FLAGS.debug and FLAGS.workers == 1,
)
server = tornado.httpserver.HTTPServer(application)
server.bind(FLAGS.port, FLAGS.address)
server.start(FLAGS.workers)

# Initialize gRPC clients after fork.
if FLAGS.cloud_debugger:
try:
import googleclouddebugger # pylint: disable=g-import-not-at-top
Expand All @@ -106,18 +124,7 @@ def main(argv: Sequence[str]) -> None:
setup_logging(handler)
py_logging.root.removeHandler(logging.get_absl_handler())

application = tornado.web.Application(
[
(r'/', MainHandler),
(r'/_ah/warmup', WarmupHandler),
(r'/recognize', RecognizeHandler, None, 'recognize'),
],
static_path=os.path.join(os.path.dirname(__file__), 'static'),
template_path=os.path.join(os.path.dirname(__file__), 'templates'),
debug=FLAGS.debug,
)
logging.info('Starting server on %s:%d', FLAGS.address, FLAGS.port)
application.listen(FLAGS.port, FLAGS.address)
logging.info('Started server on %s:%d', FLAGS.address, FLAGS.port)
tornado.ioloop.IOLoop.current().start()


Expand Down

0 comments on commit 69b85d7

Please sign in to comment.