librelist archives

« back to archive

Enable autoreload when running under Twisted

Enable autoreload when running under Twisted

From:
Tobias Oberstein
Date:
2012-02-28 @ 09:43
Hello,

I am running Flask under Twisted (see below).

This works great, but I am missing auto-reloads for developement .. can 
this be done somehow?

The line

app.debug = options.debug

below hasn't the desired effect ..

\Tobias


if __name__ == "__main__":

    parser = OptionParser ()

    parser.add_option ("-d",
                       "--debug",
                       dest = "debug",
                       action = "store_true",
                       default = False,
                       help = "Enable debug mode for Flask")

    parser.add_option ("-s",
                       "--socketserver",
                       dest = "socketserver",
                       action = "store_true",
                       default = False,
                       help = "Run Flask web app under standard Python 
SocketServer, instead of under Twisted")

    parser.add_option ("-p",
                       "--port",
                       dest = "port",
                       default = 8080,
                       help = "Listening port for Web server (i.e. 8090).")

    (options, args) = parser.parse_args ()

    if options.socketserver:
       print "Running Flask under standard Python SocketServer"
       app.run(host = "0.0.0.0", port = int(options.port), debug = 
options.debug)
    else:
       print "Running Flask under Twisted server"
       import sys
       from twisted.python import log
       from twisted.internet import reactor
       from twisted.web.server import Site
       from twisted.web.wsgi import WSGIResource

       app.debug = options.debug
       if options.debug:
          log.startLogging(sys.stdout)
       resource = WSGIResource(reactor, reactor.getThreadPool(), app)
       site = Site(resource)
       reactor.listenTCP(int(options.port), site)
       reactor.run()

Re: [flask] Enable autoreload when running under Twisted

From:
Ronny Pfannschmidt
Date:
2012-02-28 @ 09:55
Hello Tobias,

reloading is a feature of the shipped wsgi server
if you use a different wsgi server,
you need to use that wsgi servers features.

-- Ronny

On 02/28/2012 10:43 AM, Tobias Oberstein wrote:
> Hello,
>
> I am running Flask under Twisted (see below).
>
> This works great, but I am missing auto-reloads for developement .. can
> this be done somehow?
>
> The line
>
> app.debug = options.debug
>
> below hasn't the desired effect ..
>
> \Tobias
>
>
> if __name__ == "__main__":
>
>      parser = OptionParser ()
>
>      parser.add_option ("-d",
>                         "--debug",
>                         dest = "debug",
>                         action = "store_true",
>                         default = False,
>                         help = "Enable debug mode for Flask")
>
>      parser.add_option ("-s",
>                         "--socketserver",
>                         dest = "socketserver",
>                         action = "store_true",
>                         default = False,
>                         help = "Run Flask web app under standard Python
> SocketServer, instead of under Twisted")
>
>      parser.add_option ("-p",
>                         "--port",
>                         dest = "port",
>                         default = 8080,
>                         help = "Listening port for Web server (i.e. 8090).")
>
>      (options, args) = parser.parse_args ()
>
>      if options.socketserver:
>         print "Running Flask under standard Python SocketServer"
>         app.run(host = "0.0.0.0", port = int(options.port), debug =
> options.debug)
>      else:
>         print "Running Flask under Twisted server"
>         import sys
>         from twisted.python import log
>         from twisted.internet import reactor
>         from twisted.web.server import Site
>         from twisted.web.wsgi import WSGIResource
>
>         app.debug = options.debug
>         if options.debug:
>            log.startLogging(sys.stdout)
>         resource = WSGIResource(reactor, reactor.getThreadPool(), app)
>         site = Site(resource)
>         reactor.listenTCP(int(options.port), site)
>         reactor.run()

Re: [flask] Enable autoreload when running under Twisted

From:
Tobias Oberstein
Date:
2012-02-28 @ 10:01
Am 28.02.2012 10:55, schrieb Ronny Pfannschmidt:
> Hello Tobias,
>
> reloading is a feature of the shipped wsgi server
> if you use a different wsgi server,
> you need to use that wsgi servers features.
>

Hello Ronny,

ok, makes sense. Too bad ..

Thanks,
\Tobias

Re: [flask] Enable autoreload when running under Twisted

From:
Simon Sapin
Date:
2012-02-28 @ 10:16
Le 28/02/2012 10:43, Tobias Oberstein a écrit :
> Hello,
>
> I am running Flask under Twisted (see below).
>
> This works great, but I am missing auto-reloads for developement .. can
> this be done somehow?
>
> The line
>
> app.debug = options.debug
>
> below hasn't the desired effect ..

Hi,

For a pure Python server (or a Python anything, really) you can use the 
undocumented werkzeug.serving.run_with_reloader function:

https://github.com/mitsuhiko/werkzeug/blob/07d9f77839/werkzeug/serving.py#L585

But ... why? How does Twisted help for development?

Regards,
-- 
Simon Sapin

Re: [flask] Enable autoreload when running under Twisted

From:
Tobias Oberstein
Date:
2012-02-28 @ 10:23
Am 28.02.2012 11:16, schrieb Simon Sapin:
> Le 28/02/2012 10:43, Tobias Oberstein a écrit :
>> Hello,
>>
>> I am running Flask under Twisted (see below).
>>
>> This works great, but I am missing auto-reloads for developement .. can
>> this be done somehow?
>>
>> The line
>>
>> app.debug = options.debug
>>
>> below hasn't the desired effect ..
>
> Hi,
>
> For a pure Python server (or a Python anything, really) you can use the
> undocumented werkzeug.serving.run_with_reloader function:
>
> https://github.com/mitsuhiko/werkzeug/blob/07d9f77839/werkzeug/serving.py#L585

Thanks, but I guess that I can't use that with Twisted WSGI container, 
right?

>
>
> But ... why? How does Twisted help for development?
>
> Regards,

Because SocketServer gives me headaches .. it hangs from time to
time (page reloads take multiple secs), it produces exceptions like 
below ..

Exception happened during processing of request from ('192.168.1.119', 
55094)
Traceback (most recent call last):
   File "c:\Python27\lib\SocketServer.py", line 284, in 
_handle_request_noblock
     self.process_request(request, client_address)
   File "c:\Python27\lib\SocketServer.py", line 310, in process_request
     self.finish_request(request, client_address)
   File "c:\Python27\lib\SocketServer.py", line 323, in finish_request
     self.RequestHandlerClass(request, client_address, self)
   File "c:\Python27\lib\SocketServer.py", line 641, in __init__
     self.finish()
   File "c:\Python27\lib\SocketServer.py", line 694, in finish
     self.wfile.flush()
   File "c:\Python27\lib\socket.py", line 303, in flush
     self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] Eine bestehende Verbindung wurde softwaregesteuert
durch den Hostcomputer abgebrochen

Twisted WSGI does not have such probs.

In production, I'm running Apache/mod_wsgi .. no probs also ..

Re: [flask] Enable autoreload when running under Twisted

From:
Simon Sapin
Date:
2012-02-28 @ 11:43
Le 28/02/2012 11:23, Tobias Oberstein a écrit :
>> For a pure Python server (or a Python anything, really) you can use the
>> undocumented werkzeug.serving.run_with_reloader function:
>>
>> https://github.com/mitsuhiko/werkzeug/blob/07d9f77839/werkzeug/serving.py#L585
>
> Thanks, but I guess that I can't use that with Twisted WSGI container,
> right?

Why not? I have no idea. Have you tried it?

The source is not too hard to read, but the linked function works like this:

Fork a new process and run [sys.executable] + sys.argv there, with a 
special environment variable. The new process will go through the same 
setup steps and get to run_with_reloader, where it will see the variable 
and start the function in a thread. The main thread watches the files 
and when something changes, kills its own process with sys.exit(). 
Meanwhile, the parent process waits and do the whole thing again when 
its child terminates.

Unless Twisted does something unusual with sys.argv, sys.exit or 
processes, I guess it would work.


>>  But ... why? How does Twisted help for development?
>
> Because SocketServer gives me headaches .. it hangs from time to
> time (page reloads take multiple secs), it produces exceptions like
> below ..

I see. I’ve had this kind of issues too.

-- 
Simon Sapin