生成HTTP响应时,扭曲的webapp会卡住

时间:2014-07-08 15:49:04

标签: python twisted

我使用Twisted和SQLAlchemy创建了一个Web应用程序。由于SQLAlchemy与Twisted的基于回调的设计(Twisted + SQLAlchemy and the best way to do it)没有很好地协同工作,我在根资源中使用deferToThread()以便在自己的线程中运行每个请求。虽然这通常有效,但大约10%的请求会被卡住"。这意味着当我单击浏览器中的链接时,请求由Twisted处理,相应资源的代码将运行并生成HTML输出。但无论出于何种原因,该输出永远不会发送回浏览器。相反,Twisted发送HTTP标头(以及正确的Content-Length),但从不发送正文。连接只是在浏览器显示微调器图标时无限期保持打开状态。日志文件中的Twisted不会生成错误。

以下是一个最小的例子。如果要运行它,请使用.tac扩展名保存,然后运行twistd -noy example.tac。在我的服务器上,问题似乎在这段特定的代码中相对较少发生。使用while true; do wget -O- 'http://server.example.com:8080' >/dev/null; done之类的东西来测试它。

from twisted.web.server import Site
from twisted.application import service, internet
from twisted.web.resource import Resource
from twisted.internet import threads
from twisted.web.server import NOT_DONE_YET
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine, Column, Integer, String

Base = declarative_base()
class User(Base):
    '''A user account.'''
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    login = Column(String(64))


class WebInterface(Resource):

    def __init__(self):
        Resource.__init__(self)
        db_url = "mysql://user:password@mysql-server.example.com/myapp?charset=utf8"
        db_engine = create_engine(db_url, echo=False, pool_recycle=300) #discard connections after 300 seconds
        self.DBSession = sessionmaker(bind=db_engine)

    def on_request_done(self, _, request):
        '''All actions that need to be done after a request has been successfully handled.'''
        request.db_session.close()
        print('Session closed') #does get printed, so session should get closed properly


    def on_request_failed(self, err, call):
        '''What happens if the request failed on a network level, for example because the user aborted the request'''
        call.cancel()


    def on_error(self, err, request):
        '''What happens if an exception occurs during processing of the request'''
        request.setResponseCode(500)
        self.on_request_done(None, request)
        request.finish()
        return err 


    def getChild(self, name, request):
        '''We dispatch all requests to ourselves in order to be able to do the processing in separate threads'''
        return self


    def render(self, request):
        '''Dispatch the real work to a thread'''
        d = threads.deferToThread(self.do_work, request)
        d.addCallbacks(self.on_request_done, errback=self.on_error, callbackArgs=[request], errbackArgs=[request])
        #If the client aborts the request, we need to cancel it to avoid error messages from twisted
        request.notifyFinish().addErrback(self.on_request_failed, d)
        return NOT_DONE_YET


    def do_work(self, request):
        '''This method runs in thread context.'''
        db_session = self.DBSession()
        request.db_session = db_session
        user = db_session.query(User).first()

        body = 'Hello, {} '.format(user.login) * 1024 #generate some output data
        request.write(body)
        request.finish()


application = service.Application("My Testapp")
s = internet.TCPServer(8080, Site(WebInterface()), interface='0.0.0.0')
s.setServiceParent(application)

2 个答案:

答案 0 :(得分:2)

您可能没有使用SQLAlchemy关闭数据库连接或数据库中的死锁情况吗?在没有关闭连接/没有结束交易之前,我已经锁定了瓶子。

答案 1 :(得分:0)

我已经解决了这个问题。 @beiller,你的猜测非常接近它。从我的问题的源代码中可以看出,DB会话在请求处理开始后被打开,但是两个以相同(而不是反向)顺序关闭。在致电request.finish()之前关闭会话,一切都很好。