0

I'm writing a Django app that uses a management command to pull data from various sources. The plan is to run this command hourly with cron, and also have it run on user command from a view (i.e. when they add a new item that needs data, I don't want them to wait for the next hour to roll around to see results). The question is:

How can I set up this command such that if it is already currently running, it won't execute? Is there some place where I can stash a variable that can be checked by the script before execution? My current best idea is to have the command monitor stdout for a while to make sure nothing else is executing, but that seems like a hack at best. This is the only task that will be running in the background.

I'm basically trying to avoid using Celery here.

1
  • 1
    Don't sweat Celery. It's simple to set up and easy to run. I used Redis as a MQ with minimal problems if RabbitMQ is your headache. Commented Nov 17, 2014 at 13:06

2 Answers 2

1

Wrap your read function in a try/except block marking some external memoization for execution control. I'm using Redis as an example:

from redis import Redis

try:
    r_client = Redis() # assuming standard settings
    sentinel = r_client.incr("my_sentinel")
    if sentinel == 1:
        run_command()
    else:
        r_client.decr("my_sentinel")
except Exception as e:
    r_client.decr("my_sentinel")
    raise e
Sign up to request clarification or add additional context in comments.

2 Comments

I was hoping to avoid adding another data store. According to this question, atomic operations are doable in Django. Thoughts on using that instead?
Yes, that's something you can do but it requires a roundtrip to the database. You can also do it with the filesystem, store the value in a filelock. You can also use memcached (almost all the code is the same).
0

You could create a file named "i_am_running.log" at the beginning of your management command and remove it at the end if it. When running same management command, check for its' presence. In case of no exist - go further. Otherwise - abort.

1 Comment

This is too simple if it is vital to have only one running instance. It's too vulnerable to race conditions. You need something atomic to insure a single running instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.