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 / lock_app.py
Last active August 29, 2015 14:23
Python 中锁的概念,互斥锁、可重入锁、Condition 条件、使用 Event 实现线程间通信.
#!/bin/env python
import threading
import functools
global mutex
mutex = threading.Lock()
@tawateer
tawateer / WSGIServer.py
Last active August 29, 2015 14:23
WSGIServer 例子, 理解什么是 WSGI
#!/bin/env python
# Tested with Python 2.7.9, Linux & Mac OS X
import socket
import StringIO
import sys
@tawateer
tawateer / add_dns_record.py
Last active February 10, 2016 06:05
基于私钥删除 DNS 记录
#!/bin/env python
#-*- coding: utf-8 -*-
""" 此脚本作为参考:
根据私钥增加 DNS 正向和反向记录.
"""
import os
@tawateer
tawateer / closing_example.py
Last active August 29, 2015 14:23
理解 with 和 contextlib 的用法.
#/bin/env python
import urllib
import contextlib
with contextlib.closing(urllib.urlopen("http://www.baidu.com")) as x:
print x.code
@tawateer
tawateer / iter_vs_generator1.py
Last active August 29, 2015 14:23
迭代器和生成器的理解.
#!/bin/env python
#-*- coding: utf-8 -*-
"""
包含 yield 指令的函数执行的时候就是一个生成器, 其实一个协程对象,
启动后执行到 yield 指令后返回 yield 后的内容, 然后让出, 等待下一次执行.
"""
def create_generator():
@tawateer
tawateer / change_hostname.py
Created June 29, 2015 05:50
一个简单脚本,把第一列主机名改成第二列。
#!/bin/env python
# -*- coding: utf-8 -*-
import subprocess
text="""
test0 test10
test1 test11
"""
@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 / 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 / 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 / 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)