Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tornado multiprocess does not work for tensorflow #2115

Closed
alexwongdl opened this issue Jul 22, 2017 · 1 comment
Closed

Tornado multiprocess does not work for tensorflow #2115

alexwongdl opened this issue Jul 22, 2017 · 1 comment

Comments

@alexwongdl
Copy link

alexwongdl commented Jul 22, 2017

I try to write a simple frame for tornado + tensorflow server, code is as following:

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define
import os
os.environ["CUDA_VISIBLE_DEVICES"] = ""

define("port", default=18015, help="run on the given port", type=int)

import tensorflow as tf
a = tf.placeholder(tf.int32, shape=(), name="input")
asquare = tf.multiply(a, a, name="output")
config = tf.ConfigProto(device_count={"CPU": 20}, inter_op_parallelism_threads=10, intra_op_parallelism_threads=10)
sess = tf.Session(config=config)

class IndexHandler(tornado.web.RequestHandler):
    def initialize(self, thesess):
        self.sess = thesess

    def get(self):
        num = self.get_argument('num', 2)
        for i in range (100):
            ret = self.sess.run([asquare], feed_dict={a: num})
        self.write( 'result:' + str(ret))

if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(handlers=[(r"/", IndexHandler,dict(thesess=sess))])
    http_server = tornado.httpserver.HTTPServer(app)
    # http_server.listen(18824)
    # tornado.ioloop.IOLoop.current().start()

    http_server.bind(18825)
    http_server.start(10)
    tornado.ioloop.IOLoop.current().start()

subprocess starts succeed, however, the program is stuck and does not process any request. How could I make it work please. When I use simple process or multithread, the program also works.

Environment:

  1. tensorflow 1.2
  2. tornado 4.4.1
  3. python 3.5.2
@bdarnell
Copy link
Member

You may only use fork-safe libraries before entering multi-process mode (by calling http_server.start(N)). Many complex libraries are not fork-safe. You must either move the initialization of tensorflow.Session() after the HTTP server is started (in which case you will have 10 sessions, one in each process), or you can create a separate tensorflow server and connect to it using the target argument of tensorflow.Session (sessions with a target are fork-safe). The latter option is described in tensorflow/tensorflow#2448

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants