Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
Added an option: when this option is active and the message queue is …
Browse files Browse the repository at this point in the history
…empty "get" function waits for a message to be pushed into the queue
  • Loading branch information
Вадим Ливенцев authored and Вадим Ливенцев committed Feb 15, 2013
1 parent b12e2b6 commit aa7399a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ It will eventually go back onto the queue after a timeout if you don't delete it
// Stating constants several times throughout the manual is unsafe because if it gets altered
// an editor will have to change one statement and forget about the other one.

Note that all of the above will return None if the queue is empty.
```python
message = queue.get(verbose=False, period=0.5)
```
will activate persistance so that a function will not be completed until it actually recieves a message.
An attempt to pop a message will be done every "period" seconds (2 times a second in the example)


### **Delete** a message from the queue:
```python
queue.delete(message_id)
Expand Down
32 changes: 20 additions & 12 deletions iron_mq.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import iron_core
import urllib
import time
try:
import json
except:
Expand Down Expand Up @@ -87,23 +88,30 @@ def post(self, *messages):
return result['body']


def get(self, verbose=False, max=None):
def get(self, verbose=False, period=0, max=None):
"""Executes an HTTP request to get a message off of a queue.
Keyword arguments:
verbose -- If true, the entire dict is returned instead of the message body.
wait -- If true and the queue is empty, the function waits until a message appears
verbose -- If true, the entire dict is returned instead of the message body.
max -- The maximum number of messages to pull. Defaults to 1.
"""

n = ""
if max is not None:
n = "&n=%s" % max
url = "queues/%s/messages?%s" % (self.name, n)
result = self.client.get(url)
if verbose is True:
return result
else:
return result['body']
result = None
while (result == None):
n = ""
if max is not None:
n = "&n=%s" % max
url = "queues/%s/messages?%s" % (self.name, n)
result = self.client.get(url)

if period == 0:
break
sleep(period)

if verbose is True:
return result
else:
return result['body']


class IronMQ:
Expand Down

0 comments on commit aa7399a

Please sign in to comment.