Skip to content

Instantly share code, notes, and snippets.

@Motoma
Created November 10, 2011 16:27
Show Gist options
  • Save Motoma/1355294 to your computer and use it in GitHub Desktop.
Save Motoma/1355294 to your computer and use it in GitHub Desktop.
queuer.py
#!/usr/bin/env python
import os
import pickle
import time
import beanstalkc
# Images are stored locally in 'images', and moved to 'original' when
# they have been passed to the queue
IMAGE_DIRECTORY = 'images'
BACKUP_DIRECTORY = 'original'
# Hostname and port for arbiter
MQHOST = 'arbiter'
MQPORT = 11300
def main():
# Connect to the arbiter and create a message queue newImages
beanstalk = beanstalkc.Connection(host=MQHOST, port=MQPORT)
beanstalk.use('newImages')
# Iterate over images
files = os.listdir(IMAGE_DIRECTORY)
for image in files:
with open(os.path.join(IMAGE_DIRECTORY, image)) as data_h:
data = data_h.read()
# Serialize the image and pass it to the arbiter
beanstalk.put(pickle.dumps((image, data)))
# Backup the image
os.rename(os.path.join(IMAGE_DIRECTORY, image), os.path.join(BACKUP_DIRECTORY, image))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment