The uWSGI queue framework

In addition to the caching framework, uWSGI includes a shared queue.

At the low level it is a simple block-based shared array, with two optional counters, one for stack-style, LIFO usage, the other one for FIFO.

The array is circular, so when one of the two pointers reaches the end (or the beginning), it is reset. Remember this!

To enable the queue, use the queue option. Queue blocks are 8 KiB by default. Use queue-blocksize to change this.

  1. # 100 slots, 8 KiB of data each
  2. uwsgi --socket :3031 --queue 100
  3. # 42 slots, 128 KiB of data each
  4. uwsgi --socket :3031 --queue 42 --queue-blocksize 131072

Using the queue as a shared array

  1. # Put a binary string in slot 17.
  2. uwsgi.queue_set(17, "Hello, uWSGI queue!")
  3.  
  4. # Get it back.
  5. print uwsgi.queue_get(17)

Using the queue as a shared stack

Warning

Remember that uwsgi.queue_pop() and uwsgi.queue_last() will remove the item or items from the queue.

  1. # Push a value onto the end of the stack.
  2. uwsgi.queue_push("Hello, uWSGI stack!")
  3.  
  4. # Pop it back
  5. print uwsgi.queue_pop()
  6.  
  7. # Get the number of the next available slot in the stack
  8. print uwsgi.queue_slot()
  9.  
  10. # Pop the last N items from the stack
  11. items = uwsgi.queue_last(3)

Using the queue as a FIFO queue

Note

Currently you can only pull, not push. To enqueue an item, use uwsgi.queue_set().

  1. # Grab an item from the queue
  2. uwsgi.queue_pull()
  3. # Get the current pull/slot position (this is independent from the stack-based one)
  4. print uwsgi.queue_pull_slot()

Notes

  • You can get the queue size with uwsgi.queue_size.
  • Use the queue-store option to persist the queue on disk. Use queue-store-sync (in master cycles – usually seconds) to force disk syncing of the queue.
  • The tests/queue.py application is a fully working example.