Skip to content

Instantly share code, notes, and snippets.

View tawateer's full-sized avatar
Focusing

wateer tawateer

Focusing
  • Tencent
  • Beijing
View GitHub Profile
@tawateer
tawateer / kthread.py
Last active October 10, 2015 11:02
杀掉超时的线程
#-*- coding: utf-8 -*-
""" 封装成装饰器.
"""
import sys
import time
import threading
@tawateer
tawateer / BtmK.py
Last active September 14, 2015 07:36
堆排序例子
import heapq
import random
class BtmkHeap(object):
def __init__(self, k):
self.k = k
self.data = []
def Push(self, elem):
# Reverse elem to convert to max-heap
@tawateer
tawateer / graceful_shutdown_tornado_web_server.py
Last active September 6, 2015 15:18 — forked from mywaiting/graceful_shutdown_tornado_web_server.py
The example to how to shutdown tornado web server gracefully...
#!/usr/bin/env python
"""
How to use it:
1. Just `kill -2 PROCESS_ID` or `kill -15 PROCESS_ID` , The Tornado Web Server Will shutdown after process all the request.
2. When you run it behind Nginx, it can graceful reboot your production server.
3. Nice Print in http://weibo.com/1682780325/zgkb7g8k7
"""
@tawateer
tawateer / nginx_rewrite_method.conf
Created August 17, 2015 09:41
在 Nginx 配置中把接收到 T 的请求改成 GET.
server {
listen 80;
server_name {{ server_name }};
access_log {{ access_log }} main;
error_log {{ error_log }};
location / {
access_by_lua '
local reqType = ngx.var.request_method
if reqType == "T"
@tawateer
tawateer / redis_multi_connpool_client.py
Last active August 29, 2015 14:27
从 redis 多连接池中生成对象
#-*- coding: utf-8 -*-
import redis
REDIS_HOST = "localhost"
REDIS_PORT = 6379
REDIS_DB1 = 0
REDIS_DB2 = 1
REDIS_DB3 = 2
@tawateer
tawateer / futures_test.py
Last active August 29, 2015 14:27 — forked from lbolla/futures_test.py
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)
@tawateer
tawateer / parallel_timed_rotating_handler.py
Last active January 11, 2021 06:07
重写 Python logging 的 TimedRotatingFileHandler doRollover 方法,解决多进程切割日志的问题。
#!/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import logging
from logging.handlers import TimedRotatingFileHandler
LOG_DIR = "/tmp/"
@tawateer
tawateer / 1.py
Last active August 29, 2015 14:26
Python 四种单例实现方法
#-*- coding: utf-8 -*-
"""
方法1, 实现 __new__ 方法
并在将一个类的实例绑定到类变量 _instance上,
如果 cls._instance 为 None 说明该类还没有实例化过,实例化该类,并返回
如果 cls._instance 不为 None , 直接返回 cls._instance
"""
@tawateer
tawateer / nginx_log_cut.sh
Created July 3, 2015 09:30
Nginx 日志切割脚本
#!/bin/bash
# set the path to nginx log files
log_files_path="/home/work/nginx/logs/"
#log_files_dir=${log_files_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
log_files_dir="$log_files_path"
# set nginx log files you want to cut
log_files_type=(access error)
@tawateer
tawateer / change_hostname.py
Created June 29, 2015 05:50
一个简单脚本,把第一列主机名改成第二列。
#!/bin/env python
# -*- coding: utf-8 -*-
import subprocess
text="""
test0 test10
test1 test11
"""