1
我想编写一个在后台处理请求的web服务。该服务将请求放入队列并立即响应客户端。Python长期运行流程的龙卷风web服务
我在下面的代码中的问题是,BackgroundThread()。run()函数中的while循环不起作用。
虽然BackgroundThread.run()方法中的循环并不像infinite那样工作,但它只进入while循环一次。
谢谢。
代码:
class BackgroundThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global queue
while True:
item = queue.get()
if item is not None:
#long running process
time.sleep(random.randint(10, 100)/1000.0)
print "task", item, "finished"
queue = Queue.Queue()
class MyHandler(tornado.web.RequestHandler):
@gen.coroutine
def get(self):
global queue
self.write('OK')
self.finish()
filePath = self.get_arguments("filePath")
queue.put(filePath)
print queue.qsize()
if __name__=='__main__':
try:
BackgroundThread().start()
BackgroundThread().start()
app = tornado.web.Application([(r'/', MyHandler)])
print("server opened on port : 8000")
server = tornado.httpserver.HTTPServer(app)
server.bind(8000)
server.start(4) # Specify number of subprocesses
tornado.ioloop.IOLoop.current().start()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
sys.exit(1)
你是什么意思“Worker.run()函数不工作”是什么意思? – praveen
我编辑了Worker到BackgroundThread。运行函数中的无限循环并不像无限循环。它只进入while循环一次。 –