Skip to content

Instantly share code, notes, and snippets.

Avatar

三月沙 zhyq0826

View GitHub Profile
View go-type-inherit.go
package main
import (
"fmt"
)
type I interface {
Talk()
}
@zhyq0826
zhyq0826 / make_and_new.go
Created Jul 25, 2017
make 和 new 的区别
View make_and_new.go
package main
import (
"fmt"
)
type Foo struct {
age int
name string
}
@zhyq0826
zhyq0826 / interface.go
Created Jul 23, 2017
go interface description
View interface.go
package main
import (
"fmt"
)
//1
type I interface {
Get() int
Set(int)
@zhyq0826
zhyq0826 / python_consume_produce_thread.py
Last active Sep 8, 2016
python 生产者消费者模式使用condition
View python_consume_produce_thread.py
#!/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 Sep 5, 2016
python 多线程模拟竞态
View python_thread_race.py
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 Sep 5, 2016
python 多线程
View python_thread.py
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 Sep 4, 2016
python 迭代器代码示例
View python_iterator.py
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 Sep 4, 2016
使用装饰器创建单例模式
View singletion_with_decorator.py
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 Sep 4, 2016
使用 __new__ 创建单例模式
View singletion_with_new.py
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 Aug 18, 2016
python code for redis lock
View redis_lock.py
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