Let’s take a real wold example of a blog where comments need to be checked for spam. When the comment is saved in the database, we create a job in the queue with that comment data. Let’s take a django model in this case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Comment(models.Model):
name = Model.CharField()
email = Model.EmailField()
body = Model.TextField()
spam = Model.BooleanField()
queue = "Spam"
@staticmethod
def perform(comment_id):
comment = Comment.objects.get(pk=comment_id)
params = {"comment_author_email": comment.user.email,
"comment_content": comment.body,
"comment_author_name": comment.user.name,
"request_ip": comment.author_ip}
x = urllib.urlopen("http://apikey.rest.akismet.com/1.1/comment-check", params)
if x == "true":
comment.spam = True
else:
comment.spam = False
comment.save()
|
You can convert your existing class to be compatible with pyres. All you need to do is add a queue attribute and define a perform() method on the class.
To insert a job into the queue you need to do something like this:
>>> from pyres import ResQ
>>> r = ResQ()
>>> r.enqueue(Comment, 23) # Passing the comment id 23
This puts a job into the queue Spam. Now we need to fire off our workers. In the scripts folder there is an executable:
$ ./pyres_worker Spam
Just pass a comma separated list of queues the worker should poll.