Skip to content

Instantly share code, notes, and snippets.

View zhyq0826's full-sized avatar

三月沙 zhyq0826

View GitHub Profile
@zhyq0826
zhyq0826 / go-type-inherit.go
Created July 28, 2017 05:19
go type inherit
package main
import (
"fmt"
)
type I interface {
Talk()
}
@zhyq0826
zhyq0826 / make_and_new.go
Created July 25, 2017 23:31
make 和 new 的区别
package main
import (
"fmt"
)
type Foo struct {
age int
name string
}
@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)
@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 / 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_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_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 / 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 / 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 / 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