On 9/12/2010 5:10am, martvefun wrote:


On 08-12-10 18:39, Tom Evans wrote:
On Wed, Dec 8, 2010 at 4:52 PM, martvefun<martve...@gmail.com>  wrote:
Hello,

I'd like to start some threads when the server launch to listen to
events (I'm doing message passing).

To do so, I guess I should be using the __init__.py file at the root of
my project (by the way, what's the used of the other __init__.py files
in the apps)

I've tried with a simple test by only having 'print "starting server"'
into the file and the result is :

$ python manage.py runserver
starting server
starting server
Validating models...
0 errors found

Django version 1.2.3, using settings 'website.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Why the file is executed twice ? I don't want to have twice the same
threads.

Thank you

mart


__init__.py files aren't executed at the start of programs, the
presence of them denotes that a directory is a python module, and the
__init__.py is executed when that module is first imported under a
particular name.

Do you have a structure like this:

project
├── __init__.py
├── app1
│   └── __init__.py
└── app2
     ├── __init__.py
     └── models.py

and import from project.app2.models and from app2.models? That would
cause app2's __init__.py to be executed twice.

Cheers

Tom


Yes the structure of my project is more or less like that but I've not
two but 6 apps all with 'from django.db import models' in models.py
In several model (more than two), I'm using foreign keys to different
models.
For now the site doesn't do much, I'm using the database created with
the models in other files in the project folder.

So if the __init__.py is not a good place to start operations at server
startup, where should I put it ?

It seems like a good place to put it. Maybe you can test to see if the threads have been started already?

Here is a singleton which could live in your __init__.py and might help to record the state of your threading ... or anything else for that matter.

class singleton(object):
    """ designed by Oren Tirosh and Jeff Pitman """
    def __new__(self, *args, **kwargs):
        if not '_singleton' in self.__dict__:
            slate = object.__new__(self)
            slate.state = {
                'threads':True,
                # add other state things as required
            }
            self._singleton = slate
        return self._singleton

hth

Mike





--
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.

Reply via email to