Skip to content

Instantly share code, notes, and snippets.

@nicky-zs
nicky-zs / run_background.py
Last active December 21, 2015 04:08
Run costly tasks in the background in tornado, along with tornado.gen.engine.
# vim: fileencoding=utf-8
import sys, time
from multiprocessing.pool import ThreadPool
from tornado import web, gen, stack_context, httpserver, ioloop, util, options
_workers = ThreadPool()
def costly_task(n): # a costly task like processing images
time.sleep(n)
return n
@nicky-zs
nicky-zs / make_safely_shutdown.py
Created August 22, 2013 09:10
Make the tornado httpserver.HTTPServer to be shutdown safely.
# vim: fileencoding=utf-8
import time, signal
from tornado import web, ioloop, options, httpserver
_SHUTDOWN_TIMEOUT = 30
def make_safely_shutdown(server):
io_loop = server.io_loop or ioloop.IOLoop.instance()
def stop_handler(*args, **keywords):
def shutdown():
@nicky-zs
nicky-zs / multipart.py
Created September 23, 2013 05:43
Build multipart/form-data with files.
#vim: fileencoding=utf-8
import mimetools
import itertools
class MultiPartForm(object):
def __init__(self):
self.form_fields = []
self.files = []
@nicky-zs
nicky-zs / memcpy.c
Created November 19, 2013 06:31
One way to solve the glibc compatibility problem. In my case, when building a program with libthrift.a on linux with glibc version 2.15, ld always links memcpy@GLIBC_2.14 which cannot be found on systems with glibc version < 2.14. So, use this file to define a symbol __wrap_memcpy and use -Wl,--wrap=memcpy to tell ld using this symbol when meeti…
#include <string.h>
void *__memcpy_glibc_2_2_5(void *, const void *, size_t);
asm(".symver __memcpy_glibc_2_2_5, memcpy@GLIBC_2.2.5");
void *__wrap_memcpy(void *dest, const void *src, size_t n)
{
return __memcpy_glibc_2_2_5(dest, src, n);
}
@nicky-zs
nicky-zs / readline.js
Created December 19, 2013 02:47
The readline functionality is not very well supported in nodejs for it's api level is unstable as well as it is treated for tty use first. Here is a simple segment to readline from stdin. If you want to readline from a file, either changing the code with fs.createReadStream or just redirecting input from bash can do.
var readline = require('readline');
function main() {
readline.createInterface({
input: process.stdin,
terminal: false,
}).on('line', function(line) {
console.log(line);
});
}
@nicky-zs
nicky-zs / church-nums.ss
Created January 7, 2014 08:57
Church Numerals implementation in scheme.
;; A Church-Numberal is a function, which takes a function f(x)
;; as its argument and returns a new function f'(x).
;; Church-Numerals N applies the function f(x) N times on x.
; predefined Church-Numerals 0 to 9
(define zero (lambda (f) (lambda (x) x)))
(define one (lambda (f) (lambda (x) (f x))))
(define two (lambda (f) (lambda (x) (f (f x)))))
(define three (lambda (f) (lambda (x) (f (f (f x))))))
(define four (lambda (f) (lambda (x) (f (f (f (f x)))))))
@nicky-zs
nicky-zs / tool.py
Created July 9, 2014 08:21
A modification to python's standard library: json/tool.py, which makes it better print JSON containing non-ascii code.
r"""Command-line tool to validate and pretty-print JSON
Usage::
$ echo '{"json":"obj"}' | python -m json.tool
{
"json": "obj"
}
$ echo '{ 1.2:3.4}' | python -m json.tool
Expecting property name: line 1 column 2 (char 2)
@nicky-zs
nicky-zs / substitute.py
Created July 11, 2014 06:15
A simple python template renderer.
#!/usr/bin/python2.7
import sys
import string
_usage = """
Please redirect stdin to the input file, redirect stdout to the output file.
./subtitute.py ARG1=XXX ARG2=YYY < path_to_input > path_to_output\n
"""
@nicky-zs
nicky-zs / ios-app-build.sh
Created November 7, 2014 03:32
This is a bash script running on Mac OS with Xcode, which can automatically build an iOS APP from its source project.
#!/bin/bash
# .ipa path: /tmp/packapp/com.xxx.${sub_domain}/build/com.xxx.${sub_domain}.app.ipa
# required progs:
# wget;
# python2.7 ./substitute.py;
# lockfile;
# imagemagick(convert, identify);
@nicky-zs
nicky-zs / inflate_request_body.lua
Created July 17, 2016 02:57
Supporting gzipped request body.
-- lua-zlib is required
local MAX_BODY_SIZE = 16777216
ngx.ctx.max_chunk_size = tonumber(ngx.var.max_chunk_size) or 262144 -- 256KB
ngx.ctx.max_body_size = tonumber(ngx.var.max_body_size) or MAX_BODY_SIZE -- 16MB
function bad_request_response(status, msg)
local message = string.format('{"status": %d, "msg": "%s"}', status, msg)
ngx.status = ngx.HTTP_BAD_REQUEST