【问题标题】:Python Tornado web service for long running process用于长时间运行进程的 Python Tornado Web 服务
【发布时间】:2017-01-02 14:07:37
【问题描述】:

我想编写一个在后台处理请求的 Web 服务。服务将请求放入队列并立即响应客户端。

我在下面代码中的问题是 BackgroundThread().run() 函数中的 while 循环不起作用。

BackgroundThread.run() 方法中的while循环不像无限循环。它只进入一次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() 函数不起作用”是什么意思?
  • 我将 Worker 编辑为 BackgroundThread。并且运行函数中的无限循环不像无限。它只进入一次while循环。

标签: python web-services tornado


【解决方案1】:

我只是添加了 try except 块,因为当队列在 while 循环中为空时,它会得到一个异常并且不会迭代。

我得到了答案,这是代码:

class BackgroundThread(threading.Thread):
def __init__(self):
    threading.Thread.__init__(self)

def run(self):
    global queue
    print("qwerqwer0")
    while 1==1:
        print("qwerqwer1")
        print("qwerqwer2")
        try:
            item = queue.get()
            queue.task_done()
        except Queue.Empty:
            print("empty")
            pass
        if item is not None:
            print("qwerqwerqwer")
            #long running process
            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)

【讨论】:

    猜你喜欢
    • 2019-06-10
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 2011-02-27
    • 2019-08-01
    • 1970-01-01
    相关资源
    最近更新 更多