Skip to content

Instantly share code, notes, and snippets.

@Arbow
Arbow / python_enum.py
Last active January 3, 2016 05:19
python enum implement in 2.x
def enum(*sequential, **named):
""" 创建自定义枚举类型,支持两种用法:
1. enum('Success', 'Failed')
会从0开始给Success,Failed分配0,1两个值
2. enum(on=1, off=0)
指定on,off两个枚举的值
创建枚举类之后,可以通过调用 set_alias 方法设置枚举值对应的别名, 然后通过 get_alias 获取某个枚举值对应的别名
"""
@Arbow
Arbow / process_util.py
Created November 27, 2013 09:07
在tornado中不阻塞创建子进程的一个工具类
#!/usr/bin/env python
#coding: utf-8
import os
import shlex
import subprocess
import signal
import functools
import logging
import tornado
@Arbow
Arbow / jkiller.sh
Created July 29, 2013 06:00
Print java process cpu top 5 threads stack, from https://github.com/54chen/jkiller/blob/master/jkiller.sh
#!/bin/sh
export LANG="zh_CN.UTF-8";
export LC_ALL="zh_CN.UTF-8";
LOG_FILE="/tmp/jkiller.log";
JSTACK_FILE="/tmp/jstack.log";
PID="$1";
shift;
# Adapted from here: https://gist.github.com/489093
# Original credit goes to pplante and copyright notice pasted below
# Copyright (c) 2010, Philip Plante of EndlessPaths.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
@Arbow
Arbow / FailoverNodeLocator.java
Created November 8, 2012 07:38
Failover locator in spymemcached
public class FailoverNodeLocator implements NodeLocator {
private MemcachedNode[] nodes;
public FailoverNodeLocator(List<MemcachedNode> n) {
assert n.size() > 1;
nodes = n.toArray(new MemcachedNode[n.size()]);
}
public FailoverNodeLocator(MemcachedNode[] n) {
@Arbow
Arbow / gist:3787229
Created September 26, 2012 10:26
Start zookeeper cluster from program
private static void startClusterServer(String name, int id, int port) {
String dataDir = "./zk/" + name;
File dataDirectory = new File(dataDir);
if (!dataDirectory.exists())
dataDirectory.mkdirs();
File myIdFile = new File(dataDirectory, "myid");
if (!myIdFile.exists()) {
IO.write(String.valueOf(id).getBytes(), myIdFile);
}
Properties prop = new Properties();
@Arbow
Arbow / DirectByteBufferCleaner.java
Created July 19, 2012 03:22
回收DirectByteBuffer
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.security.AccessController;
import java.security.PrivilegedAction;
/**
* {@link DirectByteBufferCleaner}
*
* @author zhongl
* @created 2011-1-14
@Arbow
Arbow / patch.diff
Created June 2, 2011 09:01
Netty 3.2.4 idle event trigger patch
diff -r a0c46f1a4e8b src/main/java/org/jboss/netty/channel/socket/DefaultSocketChannelConfig.java
--- a/src/main/java/org/jboss/netty/channel/socket/DefaultSocketChannelConfig.java Thu Jun 02 14:13:48 2011 +0800
+++ b/src/main/java/org/jboss/netty/channel/socket/DefaultSocketChannelConfig.java Thu Jun 02 16:55:06 2011 +0800
@@ -17,6 +17,7 @@
import java.net.Socket;
import java.net.SocketException;
+import java.util.concurrent.TimeUnit;
import org.jboss.netty.channel.ChannelException;
@Arbow
Arbow / Thread.php
Created May 20, 2011 03:56
PHP Process Pool With Executor
<?php
//Found on http://www.alternateinterior.com/2007/05/multi-threading-strategies-in-php.html
//Modified by http://www.php-code.net
//Modified: add executor
class Thread {
var $pref ; // process reference
var $pipes; // stdio
var $buffer; // output buffer
var $output;
var $error;
@Arbow
Arbow / gist:977987
Created May 18, 2011 04:24
Java Generic
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<? extends Fruit> list = new ArrayList<Fruit>();
List<? super Apple> list1 = new ArrayList<Apple>();