Skip to content

Instantly share code, notes, and snippets.

@bdarnell
bdarnell / gist:614241
Created October 6, 2010 22:31
dowser patch
diff --git a/tools/third_party/dowser/__init__.py b/tools/third_party/dowser/__init__.py
index fa562b1..d716a2a 100644
--- a/tools/third_party/dowser/__init__.py
+++ b/tools/third_party/dowser/__init__.py
@@ -54,6 +54,7 @@ class Root:
if cherrypy.__version__ >= '3.1':
cherrypy.engine.subscribe('exit', self.stop)
self.runthread = threading.Thread(target=self.start)
+ self.runthread.daemon = True
self.runthread.start()
@bdarnell
bdarnell / django_tornado_handler.py
Created October 29, 2010 18:59
django_tornado_handler.py
# NOTE: This code was extracted from a larger class and has not been
# tested in this form. Caveat emptor.
import django.conf
import django.contrib.auth
import django.core.handlers.wsgi
import django.db
import django.utils.importlib
import httplib
import json
import logging
@bdarnell
bdarnell / gist:736778
Created December 10, 2010 20:43
benchmark: with vs try
#!/usr/bin/env python
# simple benchmark comparing with and try statements
import contextlib
import timeit
def work_pass():
pass
@bdarnell
bdarnell / result_collector.py
Created December 14, 2010 20:27
result_collector.py
import functools
import logging
import sys
class ResultCollector(object):
'''Like a BarrierCallback, but saves results passed to the callback.
>>> rc = ResultCollector()
>>> cb_foo = rc.get_callback('foo')
>>> cb_bar = rc.get_callback('bar')
@bdarnell
bdarnell / fdserver.py
Created July 9, 2011 20:41
Demonstration of sharing file descriptors across processes
#!/usr/bin/env python
"""This is a demonstration of sharing file descriptors across processes.
It uses Tornado (need a recent post-2.0 version from github) and the
multiprocessing module (from python 2.6+). To run it, start one copy
of fdserver.py and one or more copies of testserver.py (in different
terminals, or backgrounded, etc). Fetch http://localhost:8000 and
you'll see the requests getting answered by different processes (it's
normal for several requests to go to the same process under light
load, but under heavier load it tends to even out).
@bdarnell
bdarnell / debug_log_filter.py
Created July 15, 2012 20:30
Python logging filter for per-module debug logging
# A simple log filter to turn debug logging on and off on a per-module
# basis, when using tornado-style logging. Note that this works based
# on the module where the log statement occurs, not the name passed to
# logging.getLogger(), so it works even with libraries write directly
# to the root logger.
#
# One drawback to this approach (as opposed to using distinct
# per-module loggers and setting their levels appropriately) is that
# logger.isEnabledFor(DEBUG) will return true even when called from a
# module where debug output is not enabled, so modules that perform
@bdarnell
bdarnell / tornado_tulip.py
Created January 20, 2013 22:31
Proof of concept tornado/tulip integration
"""Proof of concept tornado/tulip integration.
Works with the current development branch of both projects as of
Jan 20. Current status: The tornado test suite passes cleanly
on a tulip-backed IOLoop. The tornado-backed event loop for tulip
supports the core call_later and add_reader method families, but not
the higher-level networking methods.
To run the tornado test suite on tulip, make sure both projects
and this file are on your python path and run:
@bdarnell
bdarnell / gist:8553378
Created January 22, 2014 04:12
Example for StaticFileHandler.get_absolute_path
# For http://stackoverflow.com/questions/21248222/how-can-tornado-serve-a-single-static-file-at-an-arbitrary-location/21248691?noredirect=1#comment32053685_21248691
class MyFileHandler(StaticFileHandler):
def initialize(self, file_mapping, **kwargs):
self.file_mapping = file_mapping
super(MyFileHandler, self).initialize(**kwargs)
@classmethod
def get_absolute_path(cls, root, path):
return StaticFileHandler.get_absolute_path(root, self.file_mapping.get(path, path))
@bdarnell
bdarnell / gist:8641880
Created January 27, 2014 01:24
Unix socket HTTP client via custom resolver
import socket
from tornado.concurrent import TracebackFuture, return_future
from tornado import gen
from tornado.httpclient import AsyncHTTPClient
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.netutil import bind_unix_socket, Resolver
from tornado.web import RequestHandler, Application
class HelloHandler(RequestHandler):
@bdarnell
bdarnell / coop.py
Created February 17, 2014 19:11
Sketches of potential HTTPServer callback interfaces
from tornado.concurrent import Future
from tornado import gen
from somewhere import parse_headers
class HTTPReader(object):
def __init__(self, stream):
self.stream = stream
self.header_future = Future()
self.chunk_future = None
self.read_ready = Future()