1
요청을 백그라운드에서 처리하는 웹 서비스를 작성하고 싶습니다. 서비스는 요청을 대기열에 넣고 즉시 클라이언트에 응답합니다.장기 실행 프로세스를위한 Python Tornado 웹 서비스
아래의 코드에서 문제는 BackgroundThread(). run() 함수의 루프가 작동하지 않는 것입니다.
BackgroundThread.run() 메서드의 루프가 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 to BackgroundThread를 편집했습니다. 그리고 run 함수의 무한 루프는 무한한 동작을하지 않습니다. 한 번만 while 루프에 들어갑니다. –