Guest User

Untitled

a guest
Feb 25th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. class Email(models.Model):
  2. sent = models.BooleanField(default=False)
  3. subj = models.CharField(max_length=140)
  4. msg = models.TextField()
  5.  
  6. from django.db import connection
  7. [...]
  8. class EmailLocks(object):
  9. def __init__(self):
  10. self.c = connection.cursor()
  11. def __enter__(self):
  12. self.c.execute('''lock tables my_app_email write''')
  13. def __exit__(self, *err):
  14. self.c.execute('unlock tables')
  15.  
  16. with EmailLocks():
  17. # read the email table and decide if you need to process it
  18. for e in Email.objects.filter(sent=False):
  19. # send the email
  20. # mark the email as sent
  21. e.sent = True
  22. e.save()
  23.  
  24. * * * * * /usr/bin/flock -n /tmp/fcj.lockfile /usr/bin/python /home/txuser/dev/Project1/projectnew/manage.py flocktest
Add Comment
Please, Sign In to add comment