Upgrade to Pro — share decks privately, control downloads, hide ads and more …

I <3 Redis & Cron

I <3 Redis & Cron

Lightning Talk example of how I applied Redis and Cron in a current Python project. High level explanation of how to quickly use both. Presented at San Francisco Python Meetup Nov 2013.


Melanie Warrick

November 13, 2013


  1. I <3 Redis & Cron Melanie Warrick nyghtowl.io @nyghtowl

  2. Overview Project: Created SF inspired micro-climate weather search solution. Easy

    search based on neighborhood. Challenge: Wanted daily weather results to auto- populate on a map before any searches happened. @nyghtowl
  3. What I wanted to change...

  4. What is Redis & Cron Redis (redis.io) No-SQL data structure

    server / key-value store ~ dictionary redis-server Cronjob (http://en.wikipedia. org/wiki/Cron) Unix software to automate commands at a specific time crontab -e @nyghtowl
  5. How to Initialize Redis (__init__.py) from redis import Redis if

    os.environ.get('REDISCLOUD_URL') is None: redis_db = Redis() else: # For Heroku url = urlparse.urlparse(os.environ.get('REDISCLOUD_URL')) redis_db = Redis(host=url.hostname, port=url.port, password=url.password) @nyghtowl
  6. How to Store Redis (daily_weather_results.py) from app import redis_db def

    seed_daily_weather(): neighborhoods = Location.query.limit(10) for location in neighborhoods: forecast = Weather.get_forecast(lat, lng, location) range_temp = forecast.high + u'\xb0F' +'-' + forecast.low + u'\xb0F' weather_details = { 'lat': lat, 'lng': lng, 'img_url': forecast.pic ,'temp_range':range_temp } redis_db.set((lat,lng), json.dumps(weather_details), 36000) if __name__ == '__main__': seed_daily_weather() @nyghtowl
  7. How to Retrieve Redis (views.py) @nyghtowl from app import redis_db

    @app.route(‘/map_details’, methods = [‘GET’]) def map_details(): daily_weather = [] locations = Location.query.all() for location in locations: if redis_db.exists((location.lat, location.lng)): weather_details_str = redis_db.get((location.lat, location.lng)) weather_details_json = json.loads(weather_details_str) daily_weather.append(weather_details_json) # list details for each location return json.dumps({‘locations’: daily_weather}) $.ajax({ url:’map_details’, type: ‘GET’, cache: false, dataType: ‘json’, success: ... });
  8. How to Setup Cronjob Code: SHELL=/bin/bash 0 7 * *

    * cd /Users/[computer_name]/../sun_finder_project && source . /env/bin/activate && ./env/bin/python ./daily_weather_results.py Translation: 7AM daily change directory to project file && activate virtual environment && run python on file to store redis data @nyghtowl
  9. Result

  10. Melanie Warrick nyghtowl.io @nyghtowl Code at.. github.com/nyghtowl/Sun_Finder