Skip to content

Instantly share code, notes, and snippets.

View philipcristiano's full-sized avatar

Philip Cristiano philipcristiano

View GitHub Profile
@philipcristiano
philipcristiano / gist:5854546
Last active December 18, 2015 22:29
Gunicorn worker hook with health check
def post_worker_init(worker):
env = {
'REQUEST_METHOD': 'GET',
'PATH_INFO': '/_status',
}
def start_response(*args, **kwargs):
_send_udp(args[0])
worker.wsgi(env, start_response)
@philipcristiano
philipcristiano / example_app.py
Created June 24, 2013 23:04
Minimal WSGI application
from werkzeug.wrappers import Request, Response
@Request.application
def application(request):
resp = Response('Hello World!')
if request.path == '/_status':
resp.status = '200 OK'
else:
resp.status ='404 Not Found'
return resp
@philipcristiano
philipcristiano / gunicorn_hook-py
Last active December 18, 2015 22:28
gunicorn worker hook
import socket
import time
def post_worker_init(worker):
_send_udp('200 OK\n')
def _send_udp(message):
udp_ip = "127.0.0.1"
udp_port = 4012
@philipcristiano
philipcristiano / restart.sh
Last active December 18, 2015 22:28
Restart gunicorn worker
echo 'Killing children of ' $1;
children=$(pgrep -P $1)
for child in $children
do
echo 'Killing' $child
kill $child
response=$(timeout 60 nc -w 0 -ul 4012)
if [ "$response" != '200 OK' ]; then
echo 'BROKEN'
def draw_faces(image_, faces):
"Draw a rectangle around each face discovered"
image = image_.copy()
drawable = ImageDraw.Draw(image)
for x, y, w, h in faces:
absolute_coords = (x, y, x + w, y + h)
drawable.rectangle(absolute_coords)
return image
face_buffer = 0.5 * (target_height - all_faces_height)
top_of_crop = top_of_faces - face_buffer
coords = (0, top_of_crop, target_width, top_of_crop + target_height)
def faces_from_pil_image(pil_image):
"Return a list of (x,y,h,w) tuples for faces detected in the PIL image"
storage = cv.CreateMemStorage(0)
facial_features = cv.Load('haarcascade_frontalface_alt.xml', storage=storage)
cv_im = cv.CreateImageHeader(pil_image.size, cv.IPL_DEPTH_8U, 3)
cv.SetData(cv_im, pil_image.tostring())
faces = cv.HaarDetectObjects(cv_im, facial_features, storage)
# faces includes a `neighbors` field that we aren't going to use here
return [f[0] for f in faces]
@philipcristiano
philipcristiano / faces_example.py
Created June 18, 2013 14:10
Resize and crop an image based on OpenCV detected faces
import sys
from PIL import Image, ImageDraw
try:
import cv
except ImportError:
print 'Could not import cv, trying opencv'
import opencv.cv as cv
@philipcristiano
philipcristiano / tornado_queue_connection.py
Last active January 3, 2019 03:23
Simple Tornado/Pika wrapper. Part of an internal library so it raises an error on init instead of importing
try:
import pika
from pika.adapters.tornado_connection import TornadoConnection
except ImportError:
pika = None
try:
import tornado
import tornado.ioloop
except ImportError:
@philipcristiano
philipcristiano / example.sql
Created January 3, 2013 01:32
sql-to-graphite example sql
SELECT "metric", 1+1;
SELECT "now", NOW();