[Twisted-Python] RE: Write into a persistent connection befor estopping

Boeuf, Jean-Francois jean-francois.boeuf at eads.com
Mon Jan 26 11:07:23 EST 2009


 

> -----Message d'origine-----
> De : twisted-python-bounces at twistedmatrix.com 
> [mailto:twisted-python-bounces at twistedmatrix.com] De la part 
> de Jean-Paul Calderone
> Envoyé : lundi 26 janvier 2009 16:23
> À : Twisted general discussion
> Objet : Re: [Twisted-Python] RE: Write into a persistent 
> connection beforestopping
> 
> On Mon, 26 Jan 2009 16:12:57 +0100, "Boeuf, Jean-Francois" 
> <jean-francois.boeuf at eads.com> wrote:
> >Hi,
> >
> >As i didn't get any answer to my problem i writed a little 
> test code to 
> >reproduce it (I can't divulgate the production code, and 
> then it would 
> >be to much complicated to expose the matter). You can just 
> run it using 
> >twistd -y test.py and then connect to the 8080 tcp port with 
> a browser. 
> >As we can see in the browser the connection is active and 
> periodicaly 
> >writed. The logs indicate that the conections are notifyied 
> before port 
> >and connection close, but the browser never receives the 
> notification.
> >
> >Thanks for your help
> >
> >##################test.py begins here####################### from 
> >twisted.application import internet, service from twisted.internet 
> >import reactor from twisted.web import resource, server, 
> static import 
> >threading
> >
> >application = service.Application("test")
> >
> >LISTENING   = list()
> >CHECK       = threading.Event()
> >
> >def writeToActiveConnections():
> >    while not CHECK.isSet():
> >        for request in LISTENING:
> >            request.write("active<br />")
> >        CHECK.wait(2)
> >
> >def onStop():
> >    print "ON STOP CALLED"
> >    CHECK.set()
> >    for request in LISTENING:
> >        request.write("<strong>Connection closed because of server
> >shutdown</strong>")
> >    print "ALL CONNECTIONS HAVE BEEN NOTIFIED"
> >
> >def onError(_error, _request):
> >    print "Connection Closed %s" % _error
> >    LISTENING.remove(_request)
> >
> >class ListenRessource(resource.Resource):
> >    def render_GET(self, _request):
> >        _request.write("listen connection opened<br />")
> >        LISTENING.append(_request)
> >        d = _request.notifyFinish()
> >        d.addCallback(LISTENING.remove, _request)
> >        d.addErrback(onError, _request)
> >        return server.NOT_DONE_YET
> >
> >class MainRessource(resource.Resource):
> >    def render_GET(self, _request):
> >        return """
> ><html>
> >    <head></head>
> >    <body>
> >        <h1>Test page</h1>
> >        <iframe src=/listen></iframe>
> >    </body>
> ></html>
> >        """
> >
> >class TestSite(server.Site):
> >    def __init__(self):
> >        root = static.File("/tmp")
> >        root.putChild("test", MainRessource())
> >        root.putChild("listen", ListenRessource() )
> >        server.Site.__init__(self, root)
> >
> >class TestService(internet.TCPServer):
> >    def __init__(self):
> >        internet.TCPServer.__init__(
> >            self,
> >            8080,
> >            TestSite()
> >        )
> >        reactor.callInThread(writeToActiveConnections)
> >        reactor.addSystemEventTrigger('before', 'shutdown', onStop)
> >
> >TestService().setServiceParent(application)
> 
> Twisted APIs are generally not safe to be invoked from any 
> thread except for the thread in which the reactor is running.
> 
> If you want to run some code periodically, don't do it with 
> threads and events, do it with reactor.callLater or 
> twisted.internet.task.LoopingCall.
> 
> Jean-Paul
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
> 
I'm Ok with that but it is no more than test sample code. The server does
write periodically the connection but doesn't send "<strong>Connection
closed because of server shutdown</strong>" whereas the onStop method is
correctly called!

Jean-François




More information about the Twisted-Python mailing list