librelist archives

« back to archive

Start/Stop Flask (again...)

Start/Stop Flask (again...)

From:
eduardo
Date:
2011-01-14 @ 18:58
Hi,

Recently in the "Start/Stop Flask" thread on this list, Simon Sapin 
nicelly suggested a
way to control execution of a Flask application using multiprocessing. 
It worked fine
but, what I actually need is control Flask from within my application. 
I'm starting to
learn Python and for that, I started writing a small aplication that I 
wish have it's user
interface available for access through web browsers (like POPFile).

Any suggestions?


regards,

eduardo

Re: [flask] Start/Stop Flask (again...)

From:
Simon Sapin
Date:
2011-01-15 @ 11:10
Le 15/01/2011 03:58, eduardo a écrit :
> what I actually need is control Flask from within my application.

Hi,

I you want the server to exit by itself from within a request (as 
opposed to kill it from the outside), try sys.exit()

Regards,
-- 
Simon Sapin

Re: [flask] Start/Stop Flask (again...)

From:
eduardo
Date:
2011-01-15 @ 14:04
Hi,

please be patient...

Actually, what I'm triyng is something like that:

from threading import Thread
from flask import Flask, ....

class FlaskControlThread(Thread):
     def __init__(self,params=None):
         Thread.__init__(self)
         # do init stuff...
         self.app = Flas(__name__)
     def run(self):          # I wish, I can do that
         self.app(run)
     def stop(self):         # and that
         self.app.stop()
     def restart(self):      # ...and that
         self.stop()
         self.run()
     @self.route('/')
     def index():
         # do it
     @self.route('/one')
     def one(id):
         # do that

class MyMainClass(object):
     def __init__(self, params=None):
         # do iniit stuff
         self.flask = FlaskControlThread(params=params)
         ....
         self.flask.start()
         ...

if __name__ == "__main__":
     mymainclass = MyMainClass(params='xxxxx')

regards,

eduardo

Em 15/1/2011 09:10, Simon Sapin escreveu:
> Le 15/01/2011 03:58, eduardo a écrit :
>> what I actually need is control Flask from within my application.
> Hi,
>
> I you want the server to exit by itself from within a request (as
> opposed to kill it from the outside), try sys.exit()
>
> Regards,

Re: [flask] Start/Stop Flask (again...)

From:
Simon Sapin
Date:
2011-01-15 @ 14:44
Le 15/01/2011 23:04, eduardo a écrit :
> Hi,
>
> please be patient...
>
> Actually, what I'm triyng is something like that:

This is not possible, at least not as straight-forward as this.
The thing is, Flask has no .stop() method. The built-in server is called 
with .run() which blocks, serving requests until the user hits CTRL+C.
You could work around the blocking with a thread, but threads do not 
have a .stop() or .terminate() method either. On UNIX, CTRL+C is a 
signal but you can only send signals to processes (a pid), not threads.

I’m not sure how that works, but I think Cherrypy’s server has a .stop() 
method. You could try using it instead of Flask’s, though I don’t know 
if it makes what you want possible. The app object is a WSGI application 
and can be run by any WSGI server.

If that still does not do it and you really need this, the only thing I 
see would be a having a sub-process with multiprocessing, and there 
start app.run in a deamon thread while the main thread waits for a 
message in a Queue and call sys.exit() when it gets a message from the 
queue. Your start() would be setting all this up, and your stop() 
sending the message. But this is getting very complicated for what is 
does. Can’t you do without this?

Also, you can not call .start() on the same Thread object more than 
once, so inheritance as you showed would not work. Just create new 
Thread objects and keep them as attributes.

Ho, and if you find something satisfying, you can write a snippet for 
posterity: http://flask.pocoo.org/snippets/

Regards,
-- 
Simon Sapin
http://exyr.org/

Re: [flask] Start/Stop Flask (again...)

From:
eduardo
Date:
2011-01-15 @ 15:48
Hi,

Em 15/1/2011 12:44, Simon Sapin escreveu:
> I’m not sure how that works, but I think Cherrypy’s server has a .stop()
> method. You could try using it instead of Flask’s, though I don’t know
Yeah, I was initially thinking of use Cherrypy (it looks great) but, 
it's documentation
(at least to me) is a bit confusing and, besides, when a found Flask 
(it's simplicity, documentation...) it changed my mind. That's why I'm 
trying Flask.
> if it makes what you want possible. The app object is a WSGI application
> and can be run by any WSGI server.
And I still have Flask's routing and everything else?

> If that still does not do it and you really need this, the only thing I
> see would be a having a sub-process with multiprocessing, and there
> start app.run in a deamon thread while the main thread waits for a
> message in a Queue and call sys.exit() when it gets a message from the
> queue. Your start() would be setting all this up, and your stop()
> sending the message. But this is getting very complicated for what is
> does. Can’t you do without this?
>
In fact, my application is a 'batch' application whose user interface 
will be through
a web browser (a la POPFile). Sounds crazy? I thik that this way is 
easier to do it
multiplatform. Am I wrong, is there an easier way in doing that? Anyway, 
obout one
thing I'm sure, it will be done with Python (I have to learn it).
> Ho, and if you find something satisfying, you can write a snippet for
> posterity: http://flask.pocoo.org/snippets/
>
Sure I will


regards,

   eduardo

Re: [flask] Start/Stop Flask (again...)

From:
Simon Sapin
Date:
2011-01-15 @ 15:56
Le 16/01/2011 00:48, eduardo a écrit :
> Hi,
>
> Em 15/1/2011 12:44, Simon Sapin escreveu:
>> I’m not sure how that works, but I think Cherrypy’s server has a .stop()
>> method. You could try using it instead of Flask’s, though I don’t know
> Yeah, I was initially thinking of use Cherrypy (it looks great) but,
> it's documentation
> (at least to me) is a bit confusing and, besides, when a found Flask
> (it's simplicity, documentation...) it changed my mind. That's why I'm
> trying Flask.
>> if it makes what you want possible. The app object is a WSGI application
>> and can be run by any WSGI server.
> And I still have Flask's routing and everything else?

Cherrypy is both a framework (like Flask) and a HTTP server. You can use 
just the server part with other frameworks.

> In fact, my application is a 'batch' application whose user interface
> will be through
> a web browser (a la POPFile). Sounds crazy? I thik that this way is
> easier to do it
> multiplatform. Am I wrong, is there an easier way in doing that? Anyway,
> obout one
> thing I'm sure, it will be done with Python (I have to learn it).

I would just use app.run() and leave it to the user to hit CTRL-C to exit.
If you want to have an "exit" button in your web interface, you can just 
have a view that calls sys.exit.
Why do you need those .stop() and .restart() ?

Regards,
-- 
Simon Sapin

Re: [flask] Start/Stop Flask (again...)

From:
eduardo
Date:
2011-01-16 @ 17:28
Hi,

Em 15/1/2011 13:56, Simon Sapin escreveu:
> Le 16/01/2011 00:48, eduardo a écrit :
>> Hi,
>>
>> Em 15/1/2011 12:44, Simon Sapin escreveu:
>>> I’m not sure how that works, but I think Cherrypy’s server has a .stop()
>>> method. You could try using it instead of Flask’s, though I don’t know
Yes, it has and I got Flask/Cherrypy (wsgiserver) running... But, at 
that moment, I
realized that, if the application's control will be done through web 
browsers, makes
no sense control it from outside... So I'll dive into Flask and see what 
happens.

regards,

   eduardo