OpenSource For You

Redis + Hotqueue

-

Hotnueue is a small Python library that implements queues with Redis, and is really easy to get started with. Let’s directly dive into the code: # Producer script (or the script that adds tasks # in the queue) from hotqueue import HotQueue queue = HotQueue("myqueue", host="localhost", port=6379,

db=0)

queue.put('some message/work')

The above example is very simple. There is a producer (a script that adds tasks in the queue) and a consumer or worker (a script that picks up tasks from the other end of the queue, and does the work accordingl­y). In the producer VFULSW, wH fiUVW FRQQHFW wLWh WhH 5HGLV VHUYHU, DQG WhHQ DGG the task (or message) using the put method of the eotnueue class. So whenever we run the producer script, a task gets added. The consumer script starts by connecting to the Redis VHUYHU, DQG WhHQ JRHV LQWR DQ LQfiQLWH ORRS. ,W fiUVW WULHV WR JHW a message from the queue by using the get method of the eotnueue class. This is a blocking call, which means that nothing will execute unless you have a task in the queue. As soon as a message is picked up from the queue, execution proceeds, and you may do anything with the picked up task. So in our email example, you may use the put method in, say, your cl~sk views. The actual task may be a gSOkencode­d message with the recipient’s email address, the message and the subject; and our worker script will pick that up and send the email.

Newspapers in English

Newspapers from India