Skip to content

Instantly share code, notes, and snippets.

package puzzle;
import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;
public abstract class Buffer<T> {
private ArrayList<T> _items = null;
private ArrayList<T> _prepare = null;
local ffi = require "ffi"
local tick
local n = 8000
local x = {}
for i = 1, 8000 do
x[i] = i
end
@westhood
westhood / spdy_support_test.py
Last active August 29, 2015 13:56
Use npn to test if the site supports spdy protocol
import ssl
import socket
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.set_npn_protocols(["http/1.1", "spdy/3"])
sock = socket.socket()
ssl_sock = context.wrap_socket(sock)
ssl_sock.connect(("www.google.com", 443))
@westhood
westhood / update
Created November 26, 2013 16:06
git hook script in update phrase to ensure only specific users can commit to given branches
#!/usr/bin/env python
import sys
import re
import pwd
import os
ptn = re.compile("refs/heads/(.*)")
allows_only = {
"master": ["yuanye4"]
}
@westhood
westhood / massive_insert.py
Created April 17, 2013 14:57
Use redis-cli to insert massive data in python
import subprocess
p = subprocess.Popen(["redis-cli", "--pipe"],
stdin=subprocess.PIPE)
for x in xrange(0, 100):
# write to redis-cli stdin in redis protocol
p.stdin.write("set %s %s\r\n" % (x, x + 1)
p.stdin.close()
@westhood
westhood / gist:5271478
Last active December 15, 2015 13:59
为什么 while 1:pass 要比 while True: pass 快

今天看到一个帖子说 pythonwhile 1: pass 要比 while True: pass 速度更快。

第一感觉是有点反直觉。 while 1: pass 貌似应等价于 while bool(1): pass , 怎么也要比 while True: pass 多一次转换的调用。

于是做了一个简单的实验,

In [1]: timeit while True: break
10000000 loops, best of 3: 109 ns per loop
@westhood
westhood / uuid.lua
Created March 29, 2013 15:15
Generate uuid via luajit ffi
local ffi = require("ffi")
ffi.cdef[[
typedef unsigned char uuid_t[16];
void uuid_generate(uuid_t out);
void uuid_unparse(const uuid_t uu, char *out);
]]
local libuuid = ffi.os == "OSX" and ffi.C or ffi.load("uuid")
import gevent.monkey
import logging
logger = logging.getLogger(__name__)
logger.info("Gevent monkey patch is applied")
gevent.monkey.patch_all()
# RLock of sentry log handler may be created before applying gevent monkey
# patch, which will lead to a deadlock. Replace it with a gevent RLock
@westhood
westhood / print_frames.py
Created March 29, 2013 06:00
print stack frames of a greenlet in gevent
def print_frames(greenlet):
f = greenlet.gr_frame
while f:
print f.f_code, f.f_locals
f = f.f_back
@westhood
westhood / scoped.lua
Last active December 14, 2015 09:38
Simulate go defer in lua
function scoped(...)
local args = {...}
local alias, f
if #args == 1 then
f = args[1]
alias = "defer"
elseif # args == 2 then
f = args[2]