2011-12-05 51 views
2

我新的扭曲,我不明白爲什麼我的運行腳本時我得到這個錯誤\Python的扭曲錯誤:「Request.write的請求被稱爲Request.finish被稱爲後」

基本上。 ,該腳本由2個頁面組成,第一個是調用自身的HTML表單執行阻止方法並顯示結果。當請求被髮送到服務器的同時,我得到這個錯誤:

exceptions.RuntimeError:Request.write呼籲請求Request.finish被稱爲

後,你有什麼想法在那裏可以來自 ?

class FormPage(Resource):  
    def render_GET(self, request): 
     return 
       <html> 
        <body> 
         <form method="POST"> 
          <input name="the-field" type="text" /> 
          <input type='submit' name='Get udl' /> 
         </form> 
        </body> 
       </html> 

    def render_POST(self, request): 
     deferred= deferToThread(my_blocking_method, arg1, arg2) 
     deferred.addCallback(self.print_success, request) 
     deferred.addCallback(self.print_failure, request) 
     return NOT_DONE_YET 

    def print_success(self, result, request): 
     request.write('Request executed successfully') 
     request.finish() 


    def print_failure(self, err, request): 
     request.write(str(err)) 
     request.finish()  

def run() 
    root = Resource() 
    root.putChild("form", FormPage()) 
    factory = Site(root) 
    reactor.listenTCP(8880, factory) 
    reactor.run() 

回答

1

我終於找到了解決方案。代碼必須修改如下。\ 帶有第二個addCallback的行需要更改爲addErrback。

class FormPage(Resource):  
    def render_GET(self, request): 
     return 
      <html> 
       <body> 
         <form method="POST"> 
          <input name="the-field" type="text" /> 
          <input type='submit' name='Get udl' /> 
         </form> 
        </body> 
       </html> 

    def render_POST(self, request): 
     deferred= deferToThread(my_blocking_method, arg1, arg2) 
     deferred.addCallback(self.print_success, request) 
     deferred.addErrback(self.print_failure, request) 
     return NOT_DONE_YET 

    def print_success(self, result, request): 
     request.write('Request executed successfully') 
     request.finish() 


    def print_failure(self, err, request): 
     request.write(str(err)) 
     request.finish()  

    def run() 
     root = Resource() 
     root.putChild("form", FormPage()) 
     factory = Site(root) 
     reactor.listenTCP(8880, factory) 
     reactor.run()