Skip to content

Instantly share code, notes, and snippets.

@lbolla
lbolla / futures_test.py
Last active April 24, 2023 17:59
Tornado and concurrent.futures
from concurrent.futures import ThreadPoolExecutor
from functools import partial, wraps
import time
import tornado.ioloop
import tornado.web
EXECUTOR = ThreadPoolExecutor(max_workers=4)
@lbolla
lbolla / bench_excel_writers.py
Last active August 15, 2022 21:06 — forked from jmcnamara/bench_excel_writers.py
Benchmark of several Python Excel writing modules
##############################################################################
#
# Simple Python program to benchmark several Python Excel writing modules.
#
# python bench_excel_writers.py [num_rows] [num_cols]
#
#
import sys
import resource
@lbolla
lbolla / add_reviewer_bookmarklet.js
Created October 21, 2011 09:16
Add reviewer to Gerrit
javascript:(function() {var s=document.createElement('script'); s.src="https://raw.github.com/gist/1303423/4859b59a423612c90ec5b0c77591f984c4dfdd07/add_reviewers.js"; document.getElementsByTagName('head')[0].appendChild(s);})()
@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):
[MyDB]
Description = My Database
Driver = FreeTDS
Servername = W7WS6CL832J # MSSQL server name
Database = mydb
UID = username
PWD = password
Port = 1433
Charset = UTF-8
@lbolla
lbolla / README.md
Created October 3, 2012 10:05
Asynchronous programming in Tornado

Asynchronous programming with Tornado

Asynchronous programming can be tricky for beginners, therefore I think it's useful to iron some basic concepts to avoid common pitfalls.

For an explanation about generic asynchronous programming, I recommend you one of the [many][2] [resources][3] [online][4].

I will focus on solely on asynchronous programming in [Tornado][1]. From Tornado's homepage:

@lbolla
lbolla / nice_threads.py
Last active February 1, 2020 17:39
Demonstrate nice threads in Python
import logging
import multiprocessing
import os
import random
import threading
import time
from typing import Optional
LOG_FMT = ('%(asctime)s %(levelname)s [%(name)s] '
'<%(filename)s:%(funcName)s:%(lineno)d> '
import gc
import os
import resource
def print_mem():
u = resource.getrusage(resource.RUSAGE_SELF)
print 'N={} RSS={}'.format(N, u.ru_maxrss)
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)