You don't identify yourself
When you writing a distributed application or webservice, sometimes you need to synchronize processes on different machines. For example, you may need to run some cron job once on a random machine.
If you are using MySQL, than you can ise it's GET_LOCK function, to accomplish this task. I wrote this helper to create global lock in python, using with statement:
import contextlib
@contextlib.contextmanager
@write_session
def global_lock(name, timeout, session):
result = session.execute('SELECT GET_LOCK(%s, %s)', (name, timeout)).fetchall()
result = len(result) == 1 and result[0][0]
try:
yield result and True or False
finally:
if result:
session.execute('DO RELEASE_LOCK(%s)', (name, ))
Here write_session is decorator, which passes a session argument to a function. Here I use sqlalchemy's session, but you could use MySQLdb's connection or something like that.
And here is one of usecases for function:
def cron():
with global_lock('myapp.cron_lock', 0) as acquired:
if acquired:
do_something_significant()
else:
exit_without_doing_any_job()
That's it. But make sure, to use unique name for the lock, because you may get in trouble if some other application uses same name.
Comments
Subscribe on this post's comments
No comments. Wanna be first?
First, identify yourself and come back to leave comments.
If you wish to leave comment, please, identify yourself and then come back to this page.