1
我試圖在線程內部放慢計算,並收到一個錯誤「request.finish被調用後請求調用request.write」。扭曲的錯誤:在request.finish被調用後請求調用了Request.write
我已經查看了答案,並找到完全相同的問題,但解決方案不適合我。請指教。
from twisted.web.server import Site, NOT_DONE_YET
from twisted.web.resource import Resource
from twisted.internet import reactor, threads
from twisted.python import log
import sys
import time
def SlowComputation():
time.sleep(10)
return "Lang Computation Result"
def FastComputation():
return "Fast Computation Result"
class PerformComputation(Resource):
def Success(self, request):
request.write('Success')
request.finish()
def Failure(self, request):
request.write('Failure')
request.finish()
def render_GET(self, request):
if 'fast' in request.args:
d = threads.deferToThread(FastComputation)
d.addCallback(self.Success(request))
return NOT_DONE_YET
if 'slow' in request.args:
d = threads.deferToThread(SlowComputation)
d.addCallback(self.Success(request))
return NOT_DONE_YET
log.startLogging(sys.stdout)
root = Resource()
root.putChild("calculate", PerformComputation())
factory = Site(root)
reactor.listenTCP(8880, factory, interface='localhost')
reactor.run()
感謝讓 - 保羅,你的投入是非常寶貴的。另外,我在代碼中發現了我的拼寫錯誤。 –