Python
2024-02-06
基本语法
import sys
# 不使用 __slots__
class WithoutSlots:
def __init__(self, attr1, attr2, attr3, attr4, attr5):
self.attr1 = attr1
self.attr2 = attr2
self.attr3 = attr3
self.attr4 = attr4
self.attr5 = attr5
# 使用 __slots__
class WithSlots:
__slots__ = ['attr1', 'attr2', 'attr3', 'attr4', 'attr5']
def __init__(self, attr1, attr2, attr3, attr4, attr5):
self.attr1 = attr1
self.attr2 = attr2
self.attr3 = attr3
self.attr4 = attr4
self.attr5 = attr5
# 创建对象
obj1 = WithoutSlots(10, 20, 30, 40, 50)
obj2 = WithSlots(10, 20, 30, 40, 50)
# 打印内存使用情况
print(f"Without __slots__: {sys.getsizeof(obj1)} bytes")
print(f"With __slots__: {sys.getsizeof(obj2)} bytes")
# Without __slots__: 48 bytes
# With __slots__: 72 bytes
print(obj1.attr1) # 10
print(obj1.attr2) # 20
print(obj1.attr3) # 30
print(obj1.attr4) # 40
print(obj1.attr5) # 50
obj1.attr6 = 60
print(obj1.attr6) # 60
print(obj1.__dict__)
# {'attr1': 10, 'attr2': 20, 'attr3': 30, 'attr4': 40, 'attr5': 50, 'attr6': 60}
print(obj2.attr1) # 10
print(obj2.attr2) # 20
print(obj2.attr3) # 30
print(obj2.attr4) # 40
print(obj2.attr5) # 50
obj2.attr6 = 60 # AttributeError: 'WithSlots' object has no attribute 'attr6'
print(obj2.__dict__) # AttributeError: 'WithSlots' object has no attribute '__dict__'
优势
Python 通过字典 (__dict__) 来储存对象属性,这样更加灵活,但是也会带来一些存储开销与性能开销。
如果只需要固定的属性,可以使用 __slots__ 来优化。
注意:子类会继承父类的 slots 属性,如果希望做拓展,可以在之类中添加新的 slots 属性,例如:
class Parent:
__slots__ = ['attr1']
def __init__(self, attr1):
self.attr1 = attr1
class Child(Parent):
__slots__ = ['attr2']
def __init__(self, attr1, attr2):
super().__init__(attr1)
self.attr2 = attr2
# 创建子类对象
child = Child(10, 20)
print(child.attr1) # 10
print(child.attr2) # 20
参考资料与拓展阅读
- Slotscheck: 🎰 Ensure your
__slots__ are working properly
市场
2024-02-04
《创业 7 年复盘,中美企业服务市场差异浅析》阅读笔记:
| 美国 |
中国 |
| 标准 + 专业分工 + 集成整合 |
大而全,啥都做 |
| AI 应用创新相对丰富 |
相对落后 |
| 管理标准化 |
相对落后 |
| 订阅 |
买断 |
| 云 |
Self-hosted |
| SaaS 服务 |
自研 |
| 强调 UX |
功能 |
客户、人才、基础建设有关。
Golang fasthttp
2024-01-28
个人
2024-01-22
个人
2024-01-20
最新一期《开发者周刊》上有一个言论击中我了:
有一个名词叫做"报复性熬夜",指的是有些人明明在白天疲劳不堪,晚上却不愿意早睡,宁愿在床上玩手机。
这是因为他们控制不了自己的生活,通过在晚上推迟睡觉,获得一点自己掌控时间的自由感。
-- 《报复性熬夜》
我也一直持差不多的观点,白天太多事了,工作的、家庭的,这些时间并不属于我自己,直到晚上、半夜,一片寂静中,才有片刻时间真正可以自由支配。
坐在沙发上,坐在马桶上,抑或躺在床上,刷刷抖音,打打代码。
百度百科对熬夜是这样定义的:
熬夜,汉语词语,意为深夜还不睡。泛指因事通宵或至深夜忍困不眠。出自《清平山堂话本·快嘴李翠莲记》。
熬夜是一种现代生活经常听到或者做到的一种现象,是一种危害人的身体的不良习惯,可导致一些疾病,降低效率。熬夜是不良习惯的一种,容易缺乏内源氧。
报复性熬夜,原文中讲的都是一些老生常谈的内容,伤害身体之类,只有 “获得一点自己掌控时间的自由感” 这一句话表达得非常精彩,比我之前的表达更准确。
熬夜自然是伤身体的,这每个人都知道。
挤出来的这点时间感觉才是真正的休息,从各种角色中抽身出来,做做自己。
难以抵抗这片刻自由的诱惑啊!
开发者
2024-01-18
公众号 Python 集中营发了一篇文章《盘点即将消失的五种编程语言!》,介绍了 Ruby、VB、Perl、Delphi、Haskell 这 5 种语言。
消失不消失我不知道,这里对这几种语言做个简单了解。
服务器
2024-01-17
基础环境
- Debian 操作系统
- MariaDB 集群方案、数据同步、备份与恢复
- MongoDB 集群方案
- Redis 集群方案
- Redis Cluster
- Codis Cluster
- 如果考虑内存成本,又没有那么高的性能要求,可以改用基于磁盘的 Redis 兼容服务作为替代
- PocketMQ 集群方案
- HAProxy + Keepalived
Web 服务器
- Nginx
- 后端:Golang / Python / PHP / Java / Node.js
- CDN、云存储
其他
- 通用管理后台
- 分布式任务系统
- S3 存储
- 推送系统(邮件、短信等)
Golang
2024-01-12
后期优化:
- items 按照链表的方式组织起来,按过期时间排序,加快清理速度。
或者另外设计一个数据结构,存储缓存过期时间。
放弃每次 Get 的时候判断是否过期。
package main
import (
"fmt"
"sync"
"time"
)
type CacheItem struct {
key string
value interface{}
expiration time.Time
}
type Cache struct {
items map[string]CacheItem // 缓存项
mu sync.Mutex // 锁保护并发读写
cleanupInterval time.Duration // 清理缓存的时间间隔
}
// 创建一个新的缓存实例
func NewCache(cleanupInterval time.Duration) *Cache {
c := &Cache{
items: make(map[string]CacheItem),
cleanupInterval: cleanupInterval,
}
// 启动后台清理任务
go c.startCleanup()
return c
}
// 启动后台定期清理过期缓存的任务
func (c *Cache) startCleanup() {
ticker := time.NewTicker(c.cleanupInterval)
for {
<-ticker.C
c.cleanUpExpiredItems()
}
}
// 清理过期缓存项
func (c *Cache) cleanUpExpiredItems() {
c.mu.Lock()
defer c.mu.Unlock()
// 清理有过期时间的缓存项
for key, item := range c.items {
if time.Now().After(item.expiration) {
delete(c.items, key)
}
}
// 打印清理日志(可以根据需要调整)
fmt.Println("Expired cache items cleaned up")
}
// 添加缓存项,支持过期时间
func (c *Cache) Set(key string, value interface{}, expiration time.Time) {
c.mu.Lock()
defer c.mu.Unlock()
c.items[key] = CacheItem{key: key, value: value, expiration: expiration}
}
// 获取缓存项
func (c *Cache) Get(key string) (interface{}, bool) {
c.mu.Lock()
defer c.mu.Unlock()
// 再检查有过期时间的缓存项
if item, found := c.items[key]; found {
// if item.expiration.IsZero() || time.Now().Before(item.expiration) {
// return item.value, true
// }
// // 如果缓存项已过期,则删除并返回未找到
// delete(c.items, key)
return item.value, true
}
return nil, false
}
// 删除缓存项
func (c *Cache) Delete(key string) {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.items, key)
}
func main() {
// 创建一个缓存实例,每 1 秒清理一次过期缓存
cache := NewCache(1 * time.Second)
// 设置缓存项(带过期时间)
cache.Set("key1", "value1", time.Now().Add(5*time.Second))
// 设置没有过期时间的缓存项
cache.Set("key2", "value2", time.Time{}) // 空时间表示永不过期
// 获取缓存项
if value, found := cache.Get("key1"); found {
fmt.Println("Found key1:", value)
} else {
fmt.Println("key1 not found")
}
if value, found := cache.Get("key2"); found {
fmt.Println("Found key2:", value)
} else {
fmt.Println("key2 not found")
}
// 等待 6 秒后,key1 会过期
time.Sleep(6 * time.Second)
// 再次获取缓存项
if value, found := cache.Get("key1"); found {
fmt.Println("Found key1:", value)
} else {
fmt.Println("key1 not found (after expiration)")
}
if value, found := cache.Get("key2"); found {
fmt.Println("Found key2:", value)
} else {
fmt.Println("key2 not found")
}
// 让清理任务继续运行
select {}
}
个人
2024-01-11
#1000
杜甫
文化 杜甫
2023-12-31