Skip to content

Instantly share code, notes, and snippets.

View zhyq0826's full-sized avatar

三月沙 zhyq0826

View GitHub Profile
@zhyq0826
zhyq0826 / Future_illustrate
Last active August 29, 2015 14:27 — forked from davidbirdsong/Future_illustrate
use ioloop.IOLoop.instance().add_future to create a callback for a completed Future object
from concurrent.futures import ThreadPoolExecutor
from tornado.concurrent import run_on_executor, Future
from tornado import httpclient,ioloop,web,gen
import request
import sys
httpclient.AsyncHTTPClient.configure('tornado.curl_httpclient.CurlAsyncHTTPClient', max_clients=10)
# TODO: remove
class Foo(Exception): pass
def dot():
@zhyq0826
zhyq0826 / gist:27ed09fd419b78bfcfba
Created October 31, 2015 07:10 — forked from lucasfais/gist:1207002
Sublime Text 2 - Useful Shortcuts

Sublime Text 2 – Useful Shortcuts (Mac OS X)

General

⌘T go to file
⌘⌃P go to project
⌘R go to methods
⌃G go to line
⌘KB toggle side bar
⌘⇧P command prompt
@zhyq0826
zhyq0826 / redis_lock.py
Last active August 18, 2016 10:07
python code for redis lock
import time
import redis
import logging
logger = logging.getLogger('service.redis_lock')
CONN = redis.Redis(host='localhost')
def acquire_lock(lockname, identifier, wait_time=20, timeout=15):
end = time.time() + wait_time
@zhyq0826
zhyq0826 / singletion_with_new.py
Created September 4, 2016 08:28
使用 __new__ 创建单例模式
class Singleton(object):
_instance = None
def __new__(cls, *args, **kswargs):
if cls._instance is None:
cls._instance = super(Singleton, cls).__new__(cls, *args, **kswargs)
return cls._instance
class SingletonParent(object):
_instance = {}
@zhyq0826
zhyq0826 / singletion_with_decorator.py
Created September 4, 2016 08:46
使用装饰器创建单例模式
def singleton(cls):
_instance = {}
def wrapper():
if cls not in _instance:
_instance[cls] = cls()
return _instance[cls]
return wrapper
@zhyq0826
zhyq0826 / python_iterator.py
Last active September 4, 2016 14:45
python 迭代器代码示例
class A():
def __init__(self):
self.v = [1,2,3,4,5,6]
self.step = 0
def __iter__(self):
return self
def next(self):
@zhyq0826
zhyq0826 / python_thread.py
Last active September 5, 2016 07:04
python 多线程
import urllib2
import time
from threading import Thread
def get_responses():
urls = ['http://www.baidu.com', 'http://www.amazon.cn', 'http://www.taobao.com', 'http://www.alibaba.com']
start = time.time()
for url in urls:
print url
@zhyq0826
zhyq0826 / python_thread_race.py
Created September 5, 2016 07:10
python 多线程模拟竞态
import urllib2
import time
from threading import Thread
#define a global variable
some_var = 0
class IncrementThread(Thread):
def run(self):
@zhyq0826
zhyq0826 / python_consume_produce_thread.py
Last active September 8, 2016 08:25
python 生产者消费者模式使用condition
#!/usr/bin/env python
import time
import thread
from threading import Thread, Condition, Event, Lock, currentThread
from Queue import Queue
class ItemQ(object):
def __init__(self):
@zhyq0826
zhyq0826 / interface.go
Created July 23, 2017 05:54
go interface description
package main
import (
"fmt"
)
//1
type I interface {
Get() int
Set(int)