Skip to content

Instantly share code, notes, and snippets.

@matthill
Created April 1, 2016 15:16
Show Gist options
  • Save matthill/2daa79804a17c5d101d0195caa78bd5b to your computer and use it in GitHub Desktop.
Save matthill/2daa79804a17c5d101d0195caa78bd5b to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import beanstalkc
import json
from pprint import pprint
beanstalk = beanstalkc.Connection(host='localhost', port=11300)
TUBE_NAME='alprd'
# For diagnostics, print out a list of all the tubes available in Beanstalk
print beanstalk.tubes()
# For diagnostics, print the number of items on the current alprd queue
try:
pprint(beanstalk.stats_tube(TUBE_NAME))
except beanstalkc.CommandFailed:
print "Tube doesn't exist"
# Watch the "alprd" tube, this is where the plate data is.
beanstalk.watch(TUBE_NAME)
# Loop forever
while True:
# Wait for a second to get a job. If there's a job, process it and delete it from the queue.
# If not, go back to sleep
job = beanstalk.reserve(timeout=1.0)
if job is None:
print "No plates available right now, waiting..."
else:
print "Found a plate!"
plates_info = json.loads(job.body)
# Print all the info about this plate to the console
pprint(plates_info)
# Do something with this data (e.g., match a list, open a gate, etc.)
if 'data_type' not in plates_info:
print "This shouldn't be here... all OpenALPR data should have a data_type"
elif plates_info['data_type'] == 'alpr_results':
print "This is a plate result"
elif plates_info['data_type'] == 'alpr_group':
print "This is a group result"
# Delete the job from the queue now that we have processed it
job.delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment