Skip to content

Instantly share code, notes, and snippets.

import StringIO
import cPickle
small = [{i: i} for i in xrange(9)]
medium = [{i: i} for i in xrange(999)]
large = [{i: i} for i in xrange(999999)]
def dumps(obj):
return cPickle.dumps(obj)
import random
import string
import cherrypy
class StringGenerator(object):
@cherrypy.expose
def index(self):
return """<html>
@lbolla
lbolla / zombies.py
Last active May 1, 2021 15:43
Killing zombies
import os
import time
from subprocess import Popen
def check_zombies(expected_nzombies):
# NOTE: don't use Popen() here
output = os.popen(r"ps aux | grep ' Z' | grep -v grep").read()
got_nzombies = len(output.splitlines())
if got_nzombies > 0:
@lbolla
lbolla / process_manager.py
Created January 17, 2014 15:17
Class to manage long running processes, restarting them when necessary
import concurrent.futures
class ProcessManager(object):
def __init__(self):
self.pool = concurrent.futures.ProcessPoolExecutor()
self.futures = {}
def submit(self, f, *args, **kwargs):
@lbolla
lbolla / par.py
Last active January 1, 2016 05:19
Python GIL and Cython
from threading import Thread
def busy_sleep(n):
while n > 0:
n -= 1
N = 99999999
t1 = Thread(target=busy_sleep, args=(N, ))
[MyDB]
Description = My Database
Driver = FreeTDS
Servername = W7WS6CL832J # MSSQL server name
Database = mydb
UID = username
PWD = password
Port = 1433
Charset = UTF-8
all: go.so
go.o: go.c
gcc -std=c99 -c -fPIC go.c -o go.o
go.so: go.o
gcc -shared -Wl,-soname,libgo.so -o libgo.so go.o
import sys
import tornado.ioloop
import psycopg2
import psycopg2.extensions
io_loop = tornado.ioloop.IOLoop.instance()
conn = psycopg2.connect('dbname=mytest user=lbolla password=secret')
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
# How to use PG Point type in SQAlchemy
#
# Most of the code comes from here:
# http://initd.org/psycopg/docs/advanced.html#adapting-new-types
import re
import sqlalchemy
import psycopg2
from psycopg2.extensions import adapt, register_adapter, AsIs
"""
This is a simple example of WebSocket + Tornado + Redis Pub/Sub usage.
Do not forget to replace YOURSERVER by the correct value.
Keep in mind that you need the *very latest* version of your web browser.
You also need to add Jacob Kristhammar's websocket implementation to Tornado:
Grab it here:
http://gist.github.com/526746
Or clone my fork of Tornado with websocket included:
http://github.com/pelletier/tornado
Oh and the Pub/Sub protocol is only available in Redis 2.0.0: