Skip to content

Instantly share code, notes, and snippets.

View stoensin's full-sized avatar
🎯
Focusing

Joe Stone stoensin

🎯
Focusing
  • shenzhen university
  • shenzhen
View GitHub Profile
--------------------------
Kafka核心概念 |
--------------------------
# Broker
* 一台Kafka服务器,负责消息的读,写,存
* 一个集群由多个Broker组成,一个Broker可以容纳多个Topic
* Broker与Broker之间 不存在 M/S(主从)的关系
# Topic
* 同一个Topic的消息可以分布在一个或者多个节点上
class TCPClient:
def __init__(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect(self, host, port):
'''链接Server端'''
self.sock.connect((host, port))
def send(self, data):
'''将数据发送到Server端'''
@stoensin
stoensin / re
Last active February 27, 2020 13:33
. 匹配任意字符 ^ 匹配字符串开始位置 $ 匹配字符串中结束的位置 * 前面的原子重复0次、1次、多次 ? 前面的原子重复0次或者1次 + 前面的原子重复1次或多次 {n} 前面的原子出现了 n 次 {n,} 前面的原子至少出现 n 次 {n,m} 前面的原子出现次数介于 n-m 之间 ( ) 分组,需要输出的部分
\s 匹配空白字符
\w 匹配任意字母/数字/下划线
\W 和小写 w 相反,匹配任意字母/数字/下划线以外的字符
\d 匹配十进制数字
\D 匹配除了十进制数以外的值
[0-9] 匹配一个0-9之间的数字
[a-z] 匹配小写英文字母
[A-Z] 匹配大写英文字母
@stoensin
stoensin / extree
Created February 11, 2020 10:16
python 无限级分类 1、寻找到根节点,放入到列表A中。 2、寻找共同的父节点,并放入到字典B。 3、循环遍历根节点A列表,在共同父节点字典B中取出对应的子节点列表C。 4、依次递归遍历子节点列表C,直到没有子节点列表为止。
class Tree(object):
def __init__(self, data):
self.data = data
self.root_node = []
self.common_node = {}
self.tree = []
def get_root_nodes(self) -> list:
"""
遍历data查找根节点
'''
1 当指定异常被引发时,使用on_exception装饰器重试。这里有一个例子,当出现任何requests异常时,使用指数退避(backoff.expo即退避时间指数增长):
2 当目标函数返回值符合某个特定条件时,on_predicate装饰器会安排重试。当为外部生成内容轮询资源时可能有用。
3 两个backoff装饰器都可以选择使用关键字参数on_success、on_backoff和on_giveup接受事件处理程序函数。这在报告统计或执行其他自定义日志方面可能有用。
'''
@stoensin
stoensin / base.go
Last active January 31, 2020 04:05
// 单行注释
/* 多行
注释 */
// 导入包的子句在每个源文件的开头。
// Main比较特殊,它用来声明可执行文件,而不是一个库。
package main
// Import语句声明了当前文件引用的包。
import (
@stoensin
stoensin / readwrite.lock
Created January 17, 2020 13:13
提高并发的演进思路,就在: 普通锁,本质是串行执行 读写锁,可以实现读读并发 数据多版本,可以实现读写并发 读写锁相对于单纯的互斥锁,适用性更高,可以多个线程同时占用读模式的读写锁,但是只能一个线程占用写模式的读写锁。
class RWLock(object):
def __init__(self):
self.lock = threading.Lock()
self.rcond = threading.Condition(self.lock)
self.wcond = threading.Condition(self.lock)
self.read_waiter = 0 # 等待获取读锁的线程数
self.write_waiter = 0 # 等待获取写锁的线程数
self.state = 0 # 正数:表示正在读操作的线程数 负数:表示正在写操作的线程数(最多-1)
self.owners = [] # 正在操作的线程id集合
self.write_first = True # 默认写优先,False表示读优先
-- 表结构
-- * 学生表student(id,name)
-- * 课程表course(id,name)
-- * 学生课程表student_course(sid,cid,score)
create table student(
id int unsigned primary key auto_increment,
name char(10) not null
);
insert into student(name) values('张三'),('李四');
from threading import Timer
def hello():
print("hello")
class RepeatingTimer(Timer):
'''
RepeatingTimer 的 run 方法会一直执行 while 循环体,在循环体了会执行用户传入的 function 对象,并等待指定的时间。当用户想退出定时器时,只需要调用 cancel 方法,将 flag 置为 True 便不会继续执行循环体了。这样便完成了一个还不错的循环定时器。
'''
@stoensin
stoensin / DelayeQueue
Created January 11, 2020 03:16
PriorityQueue基于 最小堆 算法的,添加和移除一个元素的耗时都是log2(n)
from Queue import PriorityQueue
from datetime import datetime
import threading
class Delayed(object):
# 返回:计划执行时间
# 单位: datetime
def plan_time(self):
pass