Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » registering app level signal handlers [RSS Feed]

#1 Dec. 16, 2010 23:01:01

dmitry b.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

registering app level signal handlers


Hi,

What is the best place to register a signal handler so that the
registration happens when the app is first initialized into django? I
have an app that needs to listen to signals generated by another app
and fire off celery tasks in response. I've tried placing the
registration code into the app's __init__.py, but since I have other
reusable code there, doing so created cyclical references when
importing modules. Is there another location within the app's code
that is guaranteed to be picked up when django is being initialized
(like an django app level init rather than python level module init)?


Thanks
Dmitry

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 16, 2010 23:28:39

Graham D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

registering app level signal handlers


On Friday, December 17, 2010 10:00:55 AM UTC+11, dmitry b wrote:
>
> Hi, What is the best place to register a signal handler so that the
> registration happens when the app is first initialized into django? I have
> an app that needs to listen to signals generated by another app and fire off
> celery tasks in response. I've tried placing the registration code into the
> app's __init__.py, but since I have other reusable code there, doing so
> created cyclical references when importing modules. Is there another
> location within the app's code that is guaranteed to be picked up when
> django is being initialized (like an django app level init rather than
> python level module init)? Thanks Dmitry


Are you talking about UNIX process signals?

If you are then you will have a problem with portability of your
application. This is because UNIX process signals are usually the domain of
the hosting environment in which you are deploying your Python web
application. For that reason, under Apache/mod_wsgi you will be blocked from
registering your own UNIX signal handlers so as to avoid problems with your
signal handler registrations interfering with the hosting environments own
use of signals to manage its processes. You can in the case of
Apache/mod_wsgi override this so you can do what you want, but you do risk
causing problems or preventing certain features of the web hosting
environment from working properly.

I would strongly suggest you find an alternate way of doing things if you
are considering using UNIX process signals.

Graham

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 17, 2010 00:04:36

Steve H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

registering app level signal handlers


On 12/16/2010 6:28 PM, Graham Dumpleton wrote:
> Are you talking about UNIX process signals?
>
He's almost certainly talking about Django signals.

I've never seen a definite recommendation as to how to do things exactly
once early on in the life of your server process, so I am afraid I
cannot offer any advice.

regards
Steve
--
DjangoCon US 2011 Portland, OR: September 6-8http://djangocon.us/--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Dec. 17, 2010 00:04:44

dmitry b.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

registering app level signal handlers


On Dec 16, 3:28 pm, Graham Dumpleton <graham.dumple...@gmail.com>
wrote:
> On Friday, December 17, 2010 10:00:55 AM UTC+11, dmitry b wrote:
>
> Are you talking about UNIX process signals?

I'm sorry, I forgot there are two ways to interpret my question. It
is about Django signals.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Dec. 17, 2010 03:16:12

W. C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

registering app level signal handlers


I usually register the signals for a given application at the bottom of that
app's model.py file.

- Craig -

On Thu, Dec 16, 2010 at 19:04, dmitry b <dmitry.ma...@gmail.com> wrote:

> On Dec 16, 3:28 pm, Graham Dumpleton <graham.dumple...@gmail.com>
> wrote:
> > On Friday, December 17, 2010 10:00:55 AM UTC+11, dmitry b wrote:
> >
> > Are you talking about UNIX process signals?
>
> I'm sorry, I forgot there are two ways to interpret my question. It
> is about Django signals.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>
>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 Dec. 18, 2010 01:10:45

dmitry b.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

registering app level signal handlers


Aren't an app's models loaded lazily (upon the first use)?

On Dec 16, 7:16 pm, "W. Craig Trader" <craig.tra...@gmail.com> wrote:
> I usually register the signals for a given application at the bottom of that
> app's model.py file.
>
> - Craig -

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#7 Dec. 18, 2010 05:38:49

Yaniv A.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

registering app level signal handlers


Maybe I'm missing something here, but why not the app's __init__.py?

(my main urls.py imports all my apps' urls.py, which means their __init__.py
is likely to be called rather early in any request, no?)

- Yaniv

On Sat, Dec 18, 2010 at 3:10 AM, dmitry b <dmitry.ma...@gmail.com> wrote:

> Aren't an app's models loaded lazily (upon the first use)?
>
> On Dec 16, 7:16 pm, "W. Craig Trader" <craig.tra...@gmail.com> wrote:
> > I usually register the signals for a given application at the bottom of
> that
> > app's model.py file.
> >
> > - Craig -
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>
>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#8 Dec. 18, 2010 09:44:47

Łukasz R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

registering app level signal handlers


Not every app needs/has urls.py to be included. "models.py" is
imported Django's get_app(). So, something like admin.autodiscover()
will also import your models. The problem with putting signal handlers
in __init__, is that it isn't the place you would really expect Model
related code to live (like post_save). It can also lead to non-obvious
cyclic imports, 'cause your handler code will most likely need some
models, etc.

On 18 December 2010 06:38, Yaniv Aknin <yaniv.ak...@gmail.com> wrote:
> Maybe I'm missing something here, but why not the app's __init__.py?
> (my main urls.py imports all my apps' urls.py, which means their __init__.py
> is likely to be called rather early in any request, no?)
>
>  - Yaniv
>
> On Sat, Dec 18, 2010 at 3:10 AM, dmitry b <dmitry.ma...@gmail.com> wrote:
>>
>> Aren't an app's models loaded lazily (upon the first use)?
>>
>> On Dec 16, 7:16 pm, "W. Craig Trader" <craig.tra...@gmail.com> wrote:
>> > I usually register the signals for a given application at the bottom of
>> > that
>> > app's model.py file.
>> >
>> > - Craig -
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To post to this group, send email to django-us...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> django-users+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>>http://groups.google.com/group/django-users?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>



--
Łukasz Rekucki

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#9 Dec. 18, 2010 21:55:51

dmitry b.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

registering app level signal handlers


On Dec 18, 1:44 am, Łukasz Rekucki <lreku...@gmail.com> wrote:
>The problem with putting signal handlers
> in __init__, is that it isn't the place you would really expect Model
> related code to live (like post_save). It can also lead to non-obvious
> cyclic imports, 'cause your handler code will most likely need some
> models, etc.

Cyclic imports is exactly the problem I've run into. I do currently
keep the signal handler registration code in __init__.py because that
the only place as far as I can tell is guaranteed to be executed at
Django's initialization. But because of cyclic dependencies, I had to
move all other code out of __init__.py. Sounds like Django needs an
init.py for every app that will be executed after all imports are
done.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#10 Dec. 18, 2010 23:36:51

Russell K.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

registering app level signal handlers


On Sun, Dec 19, 2010 at 5:55 AM, dmitry b <dmitry.ma...@gmail.com> wrote:
> On Dec 18, 1:44 am, Łukasz Rekucki <lreku...@gmail.com> wrote:
>>The problem with putting signal handlers
>> in __init__, is that it isn't the place you would really expect Model
>> related code to live (like post_save). It can also lead to non-obvious
>> cyclic imports, 'cause your handler code will most likely need some
>> models, etc.
>
> Cyclic imports is exactly the problem I've run into.  I do currently
> keep the signal handler registration code in __init__.py because that
> the only place  as far as I can tell is guaranteed to be executed at
> Django's initialization.  But because of cyclic dependencies, I had to
> move all other code out of __init__.py.   Sounds like Django needs an
> init.py for every app that will be executed after all imports are
> done.

This is something that is planned for 1.4, as a consequence of the
App-refactor work that Arthur Koziel did for the GSoC this year. See
#3591 for details.

Yours,
Russ Magee %-)

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » registering app level signal handlers [RSS Feed]

Board footer

Moderator control

Enjoy the 23rd of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support