Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
A 'shutdown' function in WSGI
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 26 - 36 of 36 - Collapse all  -  Translate all to Translated (View all originals) < Older 
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Graham Dumpleton  
View profile  
 More options Feb 21 2012, 6:31 am
From: Graham Dumpleton <graham.dumple...@gmail.com>
Date: Tue, 21 Feb 2012 22:31:43 +1100
Local: Tues, Feb 21 2012 6:31 am
Subject: Re: [Web-SIG] A 'shutdown' function in WSGI
On 21 February 2012 21:41, Tarek Ziadé <ziade.ta...@gmail.com> wrote:

In mod_wsgi at least there are fail safes such that background C
threads will force kill the process if such a lockup occurs on
shutdown.

> which it is not the case with signals, since you get a chance to properly
> stop everything beforehand.

Yes and no. For a signal handler to even be able to be triggered,
there must be Python code executing in the main thread that originally
created the main interpreter.

In an embedded system such as mod_wsgi, the main thread is never used
to handle requests and actually runs in C code blocked waiting for an
internal notification that process is being shutdown.

Is not impossible and in mod_wsgi at least a signal is used to
initiate shutdown, this coming either from itself in some cases, or
from Apache parent process in others. The signal handler then uses a
socketpair pipe to wake up the blocked main thread to begin shutdown
steps. Either way it is handled at C code level because can't rely on
Python level signal handlers to actually run.

To further complicate things, in a process with multiple sub
interpreters where would the Python signal handler even run. There is
no main thread running waiting to exit. It also can't just cause the
main Python interpreter to be exited. Simply exiting a main thread
even if it did exist wouldn't allow you to cleanup sub interpreters.

In short, embedded systems are going to be quite different to what you
are used to with pure WSGI servers. It is because it is doing all this
to ensure that reliable shutdown can occur that mod_wsgi ignores all
Python signal handler registrations by default.

That all said, technically mod_wsgi could on reception of its signal,
and if there was a registry of known WSGI applications, it could tell
them the process is being shutdown. Presence of sub interpreters makes
that a lot of fun, but would be doable.

Right now without such a registry of applications with enter/exit
methods as being discussed in this thread, the only way in mod_wsgi is
to rely on atexit.

Graham
_______________________________________________
Web-SIG mailing list
Web-...@python.org
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/python-web-sig-garchiv...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Graham Dumpleton  
View profile  
 More options Feb 21 2012, 6:38 am
From: Graham Dumpleton <graham.dumple...@gmail.com>
Date: Tue, 21 Feb 2012 22:38:53 +1100
Local: Tues, Feb 21 2012 6:38 am
Subject: Re: [Web-SIG] A 'shutdown' function in WSGI
On 21 February 2012 21:07, Simon Sapin <simon.sa...@exyr.org> wrote:

> Le 21/02/2012 10:31, Graham Dumpleton a écrit :

>> You do realise you are just reinventing context managers?

>> With this 'application' do requests.

> Indeed. I didn’t want to go too far from the initial "shutdown function"
> proposal, but actual context managers would be better.

FWIW, I have been playing with context managers in other ways to solve
per request resource cleanups issues as well. I will cover some of
what I have been doing with that in my State of WSGI 2 talk at PyCon
web summit.

I sort of wish this whole discussion could perhaps wait until the web
summit where after my talk I can perhaps discuss with interested
parties all the stuff I have been playing with around improving WSGI
rather than taking shots at little bits now when there is a lot more
to consider than just this.

Graham
_______________________________________________
Web-SIG mailing list
Web-...@python.org
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/python-web-sig-garchiv...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Antoine Pitrou  
View profile  
 More options Feb 21 2012, 7:43 am
From: Antoine Pitrou <solip...@pitrou.net>
Date: Tue, 21 Feb 2012 12:43:40 +0000 (UTC)
Local: Tues, Feb 21 2012 7:43 am
Subject: Re: [Web-SIG] A 'shutdown' function in WSGI

That's a buggy lib or program. This has nothing to do with WSGI really. The
snippet Graham showed is run at any interpreter shutdown, even when you simply
run "python" in your shell.

Regards

Antoine.

_______________________________________________
Web-SIG mailing list
Web-...@python.org
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/python-web-sig-garchiv...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tarek Ziadé  
View profile  
 More options Feb 21 2012, 8:46 am
From: Tarek Ziadé <ziade.ta...@gmail.com>
Date: Tue, 21 Feb 2012 14:46:21 +0100
Local: Tues, Feb 21 2012 8:46 am
Subject: Re: [Web-SIG] A 'shutdown' function in WSGI

No, that has to do with : please let me clean my program before you try to
kill it because I can't use signals :)

> The
> snippet Graham showed is run at any interpreter shutdown, even when you
> simply
> run "python" in your shell.

here's a very simple demo: http://tarek.pastebin.mozilla.org/1489505

Run it with plain python, and try to ctrl-C it. You won't reach atexit and
will get locked.

(here: python 2.7 / mac os)

If you use signals instead of atexit, you'll have it working.

And this pattern (a thread in the background) is pretty common -- unless I
am missing something here

Cheers
Tarek

> Regards

> Antoine.

> _______________________________________________
> Web-SIG mailing list
> Web-...@python.org
> Web SIG: http://www.python.org/sigs/web-sig
> Unsubscribe:
> http://mail.python.org/mailman/options/web-sig/ziade.tarek%40gmail.com

--
Tarek Ziadé | http://ziade.org

_______________________________________________
Web-SIG mailing list
Web-...@python.org
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/python-web-sig-garchiv...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tarek Ziadé  
View profile  
 More options Feb 21 2012, 8:51 am
From: Tarek Ziadé <ziade.ta...@gmail.com>
Date: Tue, 21 Feb 2012 14:51:09 +0100
Local: Tues, Feb 21 2012 8:51 am
Subject: Re: [Web-SIG] A 'shutdown' function in WSGI

On Tue, Feb 21, 2012 at 2:46 PM, Tarek Ziadé <ziade.ta...@gmail.com> wrote:
> here's a very simple demo: http://tarek.pastebin.mozilla.org/1489505

There are two typos but the effect remains the same since you are locked
before you reach those lines:

- atexit call worker.stop() instead  of worker.join()
- in worker.stop(), it calls worker.join() instead of worker.join(self)

--
Tarek Ziadé | http://ziade.org

_______________________________________________
Web-SIG mailing list
Web-...@python.org
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/python-web-sig-garchiv...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Antoine Pitrou  
View profile  
 More options Feb 21 2012, 9:02 am
From: Antoine Pitrou <solip...@pitrou.net>
Date: Tue, 21 Feb 2012 14:02:39 +0000 (UTC)
Local: Tues, Feb 21 2012 9:02 am
Subject: Re: [Web-SIG] A 'shutdown' function in WSGI

Tarek Ziadé <ziade.tarek@...> writes:

> here's a very simple demo: http://tarek.pastebin.mozilla.org/1489505

> There are two typos but the effect remains the same since you are locked
> before you reach those lines:
> - atexit call worker.stop() instead 
> of worker.join()
> - in worker.stop(), it calls worker.join() instead of
> worker.join(self)

I find your example convincing. I think that's worth fixing in Python (e.g. by
offering an atexit() method on Thread objects). Perhaps you can open a bug?

Regards

Antoine.

_______________________________________________
Web-SIG mailing list
Web-...@python.org
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/python-web-sig-garchiv...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tarek Ziadé  
View profile  
 More options Feb 21 2012, 9:06 am
From: Tarek Ziadé <ziade.ta...@gmail.com>
Date: Tue, 21 Feb 2012 15:06:03 +0100
Local: Tues, Feb 21 2012 9:06 am
Subject: Re: [Web-SIG] A 'shutdown' function in WSGI

On Tue, Feb 21, 2012 at 3:02 PM, Antoine Pitrou <solip...@pitrou.net> wrote:
> ...
> I find your example convincing. I think that's worth fixing in Python
> (e.g. by
> offering an atexit() method on Thread objects). Perhaps you can open a bug?

Sure yeah --  Notice that I think it's still a good idea to provide the
shutdown function in WSGI, because it gives the full control to the web
server on the ordering of events.

Cheers
Tarek

> Regards

> Antoine.

> _______________________________________________
> Web-SIG mailing list
> Web-...@python.org
> Web SIG: http://www.python.org/sigs/web-sig
> Unsubscribe:
> http://mail.python.org/mailman/options/web-sig/ziade.tarek%40gmail.com

--
Tarek Ziadé | http://ziade.org

_______________________________________________
Web-SIG mailing list
Web-...@python.org
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/python-web-sig-garchiv...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tarek Ziadé  
View profile  
 More options Feb 21 2012, 9:13 am
From: Tarek Ziadé <ziade.ta...@gmail.com>
Date: Tue, 21 Feb 2012 15:13:18 +0100
Local: Tues, Feb 21 2012 9:13 am
Subject: Re: [Web-SIG] A 'shutdown' function in WSGI

for the record: http://bugs.python.org/issue14073

--
Tarek Ziadé | http://ziade.org

_______________________________________________
Web-SIG mailing list
Web-...@python.org
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/python-web-sig-garchiv...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simon Sapin  
View profile  
 More options Feb 21 2012, 4:35 am
From: Simon Sapin <simon.sa...@exyr.org>
Date: Tue, 21 Feb 2012 10:35:15 +0100
Local: Tues, Feb 21 2012 4:35 am
Subject: Re: [Web-SIG] A 'shutdown' function in WSGI

Simon Sapin <simon.sa...@exyr.org> a écrit :

>Both of these callbacks could be called once per process (aka. space
>where requests share memory.)

Sorry, that should be once per interpreter.
--
Simon Sapin
_______________________________________________
Web-SIG mailing list
Web-...@python.org
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/python-web-sig-garchiv...

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Graham Dumpleton  
View profile  
 More options Feb 21 2012, 5:06 pm
From: Graham Dumpleton <graham.dumple...@gmail.com>
Date: Wed, 22 Feb 2012 09:06:46 +1100
Local: Tues, Feb 21 2012 5:06 pm
Subject: Re: [Web-SIG] A 'shutdown' function in WSGI
If you want to be able to control a thread like that from an atexit
callback, you need to create the thread as daemonised. Ie.
setDaemon(True) call on thread.

By default a thread will actually inherit the daemon flag from the
parent. For a command line Python where thread created from main
thread it will not be daemonised and thus why the thread will be
waited upon on shutdown prior to atexit being called.

If you ran the same code in mod_wsgi, my memory is that the thread
will actually inherit as being daemonised because request handler in
mod_wsgi, from which import is trigger, are notionally daemonised.

Thus the code should work in mod_wsgi. Even so, to be portable, if
wanting to manipulate thread from atexit, make it daemonised.

Example of background threads in mod_wsgi at:

http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Monitoring_...

shows use of setDaemon().

Graham

On 22 February 2012 00:46, Tarek Ziadé <ziade.ta...@gmail.com> wrote:

_______________________________________________
Web-SIG mailing list
Web-...@python.org
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/python-web-sig-garchiv...

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris McDonough  
View profile  
 More options Feb 22 2012, 6:16 am
From: Chris McDonough <chr...@plope.com>
Date: Wed, 22 Feb 2012 06:16:20 -0500
Local: Wed, Feb 22 2012 6:16 am
Subject: Re: [Web-SIG] A 'shutdown' function in WSGI

I've read all the messages in this thread and the traffic on the bug
entry at http://bugs.python.org/issue14073 but I'm still not sure what
to tell people who want to invoke code "at shutdown".

Do we tell them to use atexit?  If so, are we saying that atexit is
sufficient for all user-defined shutdown code that needs to run save for
code that needs to stop threads?

Is it sufficient to define "shutdown" as "when the process associated
with the application exits"?  It still seems to not necessarily be
directly correlated.

- C

_______________________________________________
Web-SIG mailing list
Web-...@python.org
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/python-web-sig-garchiv...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages < Older 
« Back to Discussions « Newer topic     Older topic »