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

Docker + tornado.ioloop.IOLoop.current().start() #3293

Open
gogasca opened this issue Jul 7, 2023 · 2 comments
Open

Docker + tornado.ioloop.IOLoop.current().start() #3293

gogasca opened this issue Jul 7, 2023 · 2 comments

Comments

@gogasca
Copy link

gogasca commented Jul 7, 2023

I'm trying to start a Docker container using a systemctl service:

Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/docker run docker run --name=agent \
      --detach \
      --net host \
      my-project/agent:local \
    python3 /agent.py

If I remove --detach my container service gets stuck in initializing.

In agent:

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

    def make_app():
        return tornado.web.Application([ (r"/", MainHandler), ])  # URL Mapping

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)    # Port Number
    tornado.ioloop.IOLoop.current().start()
    print("Ready")

I tried adding print line and I see that tornado.ioloop blocks execution and systemctl service is stuck in initialize, is there a way to execute tornado.ioloop.IOLoop.current().start() in background?

# Create a new thread and start the Tornado server in that thread
server_thread = threading.Thread(target=start_server)
server_thread.start()
print("Hello")

Seems to work fine, is ok/recommended, I will run `tornado.ioloop.IOLoop.current().stop() at exit.

Thanks

@bdarnell
Copy link
Member

bdarnell commented Jul 8, 2023

Why do you think it's "stuck in initializing"? IOLoop.start is supposed to run forever; that's the main event loop of the application. Your print("Ready") is in the wrong place; it should be just before the call to start. I'm not a systemd expert but I think in general you should use Type=exec (or simple) instead of Type=oneshot for Tornado services.

@gogasca
Copy link
Author

gogasca commented Jul 10, 2023

One of the reasons why I tried "oneshot" is because Python logic may execute or not. I will update the results of my tests and will answer "Why do you think it's "stuck in initializing"?"

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