2016-02-16 92 views
0

在Flask中使用多處理ThreadPools實現多線程功能。使用線程池時使用Flask應用程序(使用UWSGI)

我的問題是,這是行不通的。當線程池被激活時,應用程序暫停。

如果我重新啓動UWSGI服務器不能正常終止線程,所以它執行一個kill -9之前等待幾分鐘:

worker 1 (pid: 369) is taking too much time to die...NO MERCY !!!

誰能告訴我什麼,我做錯了?

我的代碼看起來像這樣(簡化了一下):

from multiprocessing.dummy import Pool as ThreadPool 

    class BattleController(): 
     pool = None 

     def __init__(self): 
      self.pool = ThreadPool(2) 

     def do_work(self, info): 
      # This function is executed by a background thread. 
      print info 

     def start_async_work(self, info): 
      self.pool.map(self.do_work, [info]) 

我的看法是這樣的:

app = Flask(__name__) 
controller = BattleController() 

@app.route('/api/test', methods=['POST']) 
def login(): 
    controller.start_async_work("Some info to process") 
    return "everything worked", 200 

回答

1

對於未來的讀者: 不可能做我想做的事情(正如Oin指出的那樣),但我找到了一個替代方案: Celery看起來像一個不錯的Flask plug-in,使後臺任務。