On Thu, Feb 5, 2015 at 2:50 PM, Roberto De Ioris <[email protected]> wrote:
>
>> is there some way to restrict the signal to be registered only once?
>
> Put it in wsgi.py or place in a dedicated file you can import with
>
> --shared-pyimport file


thanks!

in this specific case i'd rather not import these functions too early,
since i need most of the Django environment running.  I'm sure it can
be done with a little care, but it's not something i want to do right
now.

Since these periodic tasks can be quite long, but i don't want more
than one running at the same time, i did a simple decorator to ensure
non-overlapping execution.  so now i don't care (too much) if the
signal is emitted too often.

my non-overlap decorator is simply:

def unique_on(name, lockdir='/tmp'):
    fpath = os.path.join(lockdir, name)
    def decorator(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            with open(fpath, 'w+') as lockfile:
                try:
                    flock(lockfile, LOCK_EX+LOCK_NB)
                except IOError:
                    return
                return f(*args, **kwargs)
        return wrapper
    return decorator


so now my scan function is like:

@timer(60)
@unique_on('scan')
def scan()
    for hf in HotFolder.objects.filter(active=True):
        .....


and now i'm sure i run only one scan process at a time.


-- 
Javier
_______________________________________________
uWSGI mailing list
[email protected]
http://lists.unbit.it/cgi-bin/mailman/listinfo/uwsgi

Reply via email to