2015-11-06 171 views
0

我有一個龍捲風應用程序,它可以上傳大型文本文件(大約500,000行),然後對其進行一些處理,最後將數據插入到MySQL表中。顯示龍捲風請求的進度

我這樣做,但我想顯示過程的進展。換句話說,由於該過程需要很長時間,所以我想向用戶顯示處理了多少文件記錄以及將多少記錄插入到表中。

我想也許有可能製作一個json文件,然後寫入進度,然後用AJAX讀取內容並在頁面中顯示。

有沒有更好的方法?

回答

1

最後,我得到了答案。

我用WebSocket查看上傳文件的進度。

也就是說,我首先用處理程序上傳文件,然後當網頁呈現給瀏覽器時,我打開一個套接字連接並用線程啓動進程。

事情是這樣的:

class MyProcessWebSocket(WebSocketHandler): 
    def __init__(self, application, request, **kwargs): 
     super(ProcessBillWebSocket, self).__init__(application, request, **kwargs) 
     self.fid = None 

    def do_process(self, z, name): 
     res = dict(percent=0) 
     self.write_message(res) 

     # do some process and self.write_message(res) in each loop. 

     self.close() 

    def open(self, *args, **kwargs): 
     try: 
      self.fid = args[0] if args[0] else None 
     except: 
      pass 
     if not self.fid: 
      self.close() 
      return 

     # Open file with self.fid that sent from rendered webpage. 

     filename = os.path.join(sh.web['static_address'], 'tmp', '%s.txt' % self.fid) 

     try: 
      z = ProcessClass.read_file(filename) 
      t = threading.Thread(target=self.do_process, args=(z, self.fid)) 
      t.start() 
     finally: 
      os.remove(filename) 


    def on_message(self, message): 
     # nothing to do 
     pass 

    def on_close(self): 
     print("connection closed.") 

我不知道這是很好的方式或沒有,但我的問題已經解決了。

Is Any other idea ???