Frage

I tried to inspire me from a couple of example, but sorry, thread are not already clear to me... :-(

I have a class that launch a tornado server, the launch is supposed to happend in a thread, so my app can do something else (webserver is only there for controlling via rest api).

#!/usr/bin/env python
import tornado.ioloop
import tornado.web
from threading import Thread

class rest(tornado.web.Application) :
    def __init__(self):
    print("initialize tornado app")
    handlers = [
       (r"/getTemperature", getTemperature) 
    ]
    super(rest,self).__init__(handlers)

    def start(self):
    print("start webserver")
    self.listen(8888)
    serverT=Thread(target=tornado.ioloop.IOLoop.instance().start())
    serverT.daemon=True
    serverT.start()

    def stop(self):
    self.cancelled=True
    tornado.ioloop.IOLoop.instance().stop()



class getTemperature(tornado.web.RequestHandler):        
    def get(self):
    print("getTemperature handler") 
    self.post()
    """Nothing to do, data are only posted"""
    def post(self):
    """Return the list of all sensors with temp"""
    self.write("this is a test from the request handler")

And I calling the class like this :

myRest=rest().start()
print "test"

But the code is only executing the webserver, and does not execute the rest : print... What do I make wrong (again ;-) ?

War es hilfreich?

Lösung

The problem is this line: serverT=Thread(target=tornado.ioloop.IOLoop.instance().start()). You're calling the start method here, but instead you need to pass the start method itself as the thread's target so the thread can call it. Just remove the last set of parens on the line: serverT=Thread(target=tornado.ioloop.IOLoop.instance().start)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top