#355 Python 打开文件的方式

2020-01-10

有同事排查 Python 项目问题的时候指出一处 open 没有关闭可能会导致句柄泄露 Handle Leak。
PS: 句柄泄漏的危害:大量资源占用可能导致性能下降,甚至由于可打开文件数达到极限,服务无法继续向外提供服务。

我看了之后告诉他,此处函数退出之后句柄会自动关闭,他还不信,下去自己研究了一会儿,可能是百度一下,过一会儿说好像确实是这样,不过他仍然很疑惑,那么 with open 的作用是什么呢?

一般我们常用上下文管理的方式(with open)来打开文件,这样可以自动关闭句柄,这是一个好的实践。

  1. 退出函数之后文件描述符的自动关闭是 CPython GC (垃圾回收机制) 的特性,非 Python 语言规范。
  2. 根据 CPython 的 GC 策略(引用计数),如果有引用,文件描述符不会被关闭,这是一个非常严重的潜在风险。
    大部分时候我们可能没有引用文件描述符,但是不能排除可能性。
    万一出现句柄泄漏,在代码库中排查可能的未关闭引用会比较麻烦。
  3. 上下文管理会自动处理异常,相当于我们的 close 方法放在 finally 块中。

无关的事情:进程退出时的句柄

进程退出时如果有没有关闭的句柄,

  1. 如果程序正常退出,
  2. 语言可能会处理一道,清理相关数据
  3. 系统会处理一道
  4. 如果程序异常退出,则只有靠系统了

至少我看到 POSIX 中有相关规定,无论任何原因或任何方式的退出,都应该:

All of the file descriptors, directory streams, conversion descriptors, and message catalog descriptors open in the calling process shall be closed.

PS: 其中提到的:

  1. 文件描述符 File Descriptors
  2. 目录流 Directory Streams
  3. 转换描述符 Conversion Descriptors
  4. 消息编码描述符 Message Catalog Descriptors

后面三个是个啥?

#354 Shell 历史

2020-01-07

参考: https://en.wikipedia.org/wiki/Comparison_of_command_shells

Linux Shell

  • Thompson shell (sh), 1971, Unix v1 - v6
    只存在历史中
    在 Bourne shell 之前,贝尔实验室还出了一个 Mashey shell,只出现在 1976 年的 PWB UNIX 中(所以又叫 PWB shell),没有大范围使用
  • Bourne shell (sh), 1977, Unix v7
  • C shell (csh), 1978 img
  • TENEX C shell (tcsh), 1983 img
    是 TENEX 系统开发的 csh 兼容 Shell
  • Korn shell (ksh), 1983 img
  • Bash, 1989 img
  • Almquist shell (ash), 1989 img
  • zsh, 1990 img
  • Debian Almquist shell (dash), an ash fork, 1997
    主体随 ash 是 BSD 协议,不过,可能是为了保持对 bash 的兼容,采用了 bash 项目的一个文件
  • fish, 2005 img

分类

  • ksh, bash, zsh, ash, dash 都是 Bourne shell 兼容。
  • csh, tcsh 就是 csh 兼容。
  • fish

说明

  1. Unix 上的 sh 有版权问题, 所以 BSD 和 Linux 上的 /bin/sh 都指向某一种兼容 Shell (一般是默认 Shell)
  2. RHEL/CentOS 上,默认 Shell 是 Bash
  3. Debian/Ubuntu 上,默认 Shell 是 Dash
  4. FreeBSD 上默认采用 tcsh, 基于 FreeBSD 的 GhostBSD 采用 fish
  5. OpenBSD 上默认采用 pdksh (pd: Public Domain), 不知道和 tsh 什么关系。可能是 OpenBSD 维护的 tsh 兼容 Shell。
  6. NetBSD 上默认采用 ash
  7. 由于 ash 非常轻量级,BusyBox 采用了 dash fork
    然后 BusyBox 被 Alpine Linux, Tiny Core Linux 或者其他嵌入式 Linux (比如 OpenWrt) 采用,所以这些系统的默认 Shell 应该就是 ash

所以可能使用最广泛的 Shell 是 ash 和 bash。

其他:MacOS 上曾经默认采用 bash, 后来专向了 zsh

Windows 平台

  • command.com, 1980
  • cmd.exe, 1993
  • PowerShell, 2006

#353 玩具总动员

2020-01-06
  1. 正片 Toy Story (1995)
    巴斯光年,阿薛
  2. Toy Story Treats (1996) 电视剧
  3. 正片 Toy Story 2 (1999)
    公鸡人,翠丝
  4. Buzz Lightyear of Star Command (2000–2001) 电视剧
  5. Buzz Lightyear of Star Command: The Adventure Begins (2000)
  6. 正片 Toy Story 3 (2010)
    抱抱熊
    故事的最后,玩具们都送给了邦妮
  7. Toy Story Toons (2011–2012) 玩具们在邦妮家的故事
  8. Hawaiian Vacation 短片:夏威夷假期
  9. Small Fry 短片:小玩具
  10. Partysaurus Rex 短片:派对恐龙
  11. Toy Story of Terror! (2013) 短片:惊魂夜
  12. Toy Story That Time Forgot (2014) 短片:遗忘时光
  13. Forky Asks a Question (2019–2020) 电视剧
  14. 正片 Toy Story 4 (2019)
    邦妮害怕上学,叉叉
    胡迪最后跟宝贝走了
  15. Lamp Life (2020) 短片:牧羊女大冒险
  16. Pixar Popcorn (2021) 一分钟短片,其中有三部玩具总动员相关的短片。
    1. To Fitness and Beyond
    2. Fluffy Stuff with Ducky and Bunny: Love
    3. Fluffy Stuff with Ducky and Bunny: Three Heads
  17. Lightyear (2022)

参考资料与拓展阅读

#352 Python DataClass

2020-01-02
import dataclasses

@dataclasses.dataclass
class User:
    user_id: int
    user_name: str
    first_name: str
    last_name: str
    age: int = 0

    def __post_init__(self):
        self.full_name = f'{self.first_name} {self.last_name}'

u = User(1, 'admin', 'Jim', 'Green')
print(u)
# User(user_id=1, user_name='admin', first_name='Jim', last_name='Green', age=0)
print(u.full_name)
# Jim Green
u.full_name = 'Han Meimei'
print(u.full_name)
# Han Meimei
print(dataclasses.asdict(u))
# {'user_id': 1, 'user_name': 'admin', 'first_name': 'Jim', 'last_name': 'Green', 'age': 0}
print(dataclasses.astuple(u))
# (1, 'admin', 'Jim', 'Green', 0)

相当于:

class User:
    def __init__(self, user_id: int, user_name: str, first_name: str, last_name: str, age: int = 0):
        self.user_id = user_id
        self.user_name = user_name
        self.first_name = first_name
        self.last_name = last_name
        self.full_name = f'{self.first_name} {self.last_name}'
        self.age = age

    def __str__(self):
        return f"{self.__class__.__name__}(user_id={self.user_id}, user_name='{self.user_name}', first_name='{self.first_name}', last_name='{self.last_name}, age={self.age}')"

    def asdict(self):
        return {
            'user_id': self.user_id,
            'user_name': self.user_name,
            'first_name': self.first_name,
            'last_name': self.last_name,
            'age': self.age,
        }

    def astuple(self):
        return self.user_id, self.user_name, self.first_name, self.last_name, self.age,

u = User(1, 'admin', 'Jim', 'Green')
print(u)
print(u.full_name)
u.full_name = 'Han Meimei'
print(u.full_name)
print(u.asdict())
print(u.astuple())

其他特性

@dataclasses.dataclass(*, init=True, repr=True, eq=True, order=False,
    unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False,
    weakref_slot=False)
  • init 是否生成 __init__ 方法(默认)
  • repr 是否生成 __repr__ 方法(默认)
  • eq 是否生成 __eq__ 方法(默认)
  • order 是否生成 __lt____le____gt____ge__ 方法
  • unsafe_hash 是否生成 __hash__ 方法
    能 hash 的是不会变化对象,否则会出问题,需要谨慎使用。
  • frozen 是否允许对字段赋值
  • match_args
  • kw_only 是否仅关键词传参
  • slots 是否生成 __slots__ 方法
  • weakref_slot 是否添加 __wrakref__ 槽位(弱引用)
    指定 weakref_slot=True 而不同时指定 slots=True 将会导致错误。

#351 “买办” 是个啥?

2020-01-01

“买办” 这个词偶尔见到,给我的感觉是只管赚钱,不爱国甚至损害国家利益和民族利益来牟利的商人。

买办是指在清代五口通商地区被西方公民雇佣并且参与其主要经营活动的中国人,原对应之葡萄牙文Comprar意译为“购买”的意思,英文则作Comprador。清代买办分为洋行买办、外商银行买办、轮船公司买办,多以广东和江浙出身。这类由外商雇用之商人通常外语能力强,一方面可作为欧美商人与中国商人的翻译,也可处理欧美国家商界与中国政府之双向沟通。除此,这类型商人还可自营商铺,因此致富者颇众。

著名买办

  • 吴健彰
  • 杨坊:早期任职于怡和洋行,英语极为流利,后成为上海最早的大买办,帮助华尔洋枪队筹集资金抵抗太平天国进攻上海。
  • 陈廉伯
  • 何甘棠
  • 卢亚贵
  • 冯明珊:有利银行。
  • 郭甘章:广东香山县买办,1870年代初,名下有甘章船厂。
  • 唐廷枢
  • 张嘉璈
  • 莫仕扬:莫仕扬家族。
  • 吴炳垣 (阿伟)
  • 何东
  • 郑观应

从移民社会转型为定居社会以后,因为台湾1860年代开港通商,在传统的地主与郊商之外,买办成为台湾新兴的社会领导阶层:

  • 李春生:台湾19世纪中期的富商。其买办身份,延续到台湾日治时期。
  • 陈福谦:台湾打狗(今高雄)。
  • 陈中和

维基百科的定义比较中性:帮助外国人与国内的企业和政府打交道,做生意。

由于在清末,一直到建国,一直存在着一个不小的买办阶层,他们甚至和政治势力结合,形成官僚买办阶层。
列强想瓜分中国,政府贪污腐败不作为,和他们纠缠在一起的买办自然也没有好名声,他们的生意肯定有很大一部分会损害国家和人民的利益。

总结一下:

  1. 有一定规模
  2. 中介性质,在国外资本和国内市场之间活动
  3. 在我们的语境中,应该再加上一条:出卖国家和人民利益

#348 GNU/Linux 上如何快速创建一个大文件?「from StackOverflow」

2019-12-25

StackOverflow 上看到好些种快速创建文件的命令,逐个测试,在 Ubuntu 下可用的方法有以下几种:

time dd if=/dev/zero of=test.img bs=10M iflag=fullblock,count_bytes count=10G
# 0.00s user 0.45s system 1% cpu 39.144 total

# 在 xfsprogs 包中:
# -n 表示不写入数据
time xfs_mkfile -n 10g test.img
# 0.01s user 0.01s system 2% cpu 0.669 total

# 在 VBox 挂载的虚拟磁盘上执行时遇到 “不支持的操作” 错误
# 在 EXT4 磁盘上没有遇到问题,速度很快
time fallocate -l 10G test.img

time truncate -s 10G test.img
# 0.00s user 0.00s system 46% cpu 0.005 total

time dd if=/dev/zero of=test.img bs=1 count=0 seek=10G
# 0.00s user 0.00s system 77% cpu 0.002 total

最后看到的 dd seek 方案深得征信深得朕心。

根据这个思路,Python 创建 10G 的文件应该这么写(也是我一直以来的写法):

GB = 1 << 30
with open('test.img', 'w') as _file:
    _file.seek(10 * GB - 1)
    _file.write(chr(0))

#347 Systemd 相关命令

2019-12-21
apt-file list systemd | grep bin/
systemd: /bin/journalctl
systemd: /bin/loginctl
systemd: /bin/networkctl
systemd: /bin/systemctl
systemd: /bin/systemd
systemd: /bin/systemd-ask-password
systemd: /bin/systemd-escape
systemd: /bin/systemd-inhibit
systemd: /bin/systemd-machine-id-setup
systemd: /bin/systemd-notify
systemd: /bin/systemd-sysext
systemd: /bin/systemd-sysusers
systemd: /bin/systemd-tmpfiles
systemd: /bin/systemd-tty-ask-password-agent
systemd: /usr/bin/bootctl
systemd: /usr/bin/busctl
systemd: /usr/bin/hostnamectl
systemd: /usr/bin/kernel-install
systemd: /usr/bin/localectl
systemd: /usr/bin/resolvectl
systemd: /usr/bin/systemd-analyze
systemd: /usr/bin/systemd-cat
systemd: /usr/bin/systemd-cgls
systemd: /usr/bin/systemd-cgtop
systemd: /usr/bin/systemd-cryptenroll
systemd: /usr/bin/systemd-delta
systemd: /usr/bin/systemd-detect-virt
systemd: /usr/bin/systemd-id128
systemd: /usr/bin/systemd-mount
systemd: /usr/bin/systemd-path
systemd: /usr/bin/systemd-resolve
systemd: /usr/bin/systemd-run
systemd: /usr/bin/systemd-socket-activate
systemd: /usr/bin/systemd-stdio-bridge
systemd: /usr/bin/systemd-umount
systemd: /usr/bin/timedatectl
  • journalctl
  • loginctl
  • networkctl
  • systemctl
  • systemd
  • systemd-ask-password
  • systemd-escape
  • systemd-inhibit
  • systemd-machine-id-setup
  • systemd-notify
  • systemd-sysext
  • systemd-sysusers
  • systemd-tmpfiles
  • systemd-tty-ask-password-agent
  • bootctl
  • busctl
  • hostnamectl
  • kernel-install
  • localectl
  • resolvectl
  • systemd-analyze
  • systemd-cat
  • systemd-cgls
  • systemd-cgtop
  • systemd-cryptenroll
  • systemd-delta
  • systemd-detect-virt
  • systemd-id128
  • systemd-mount
  • systemd-path
  • systemd-resolve
  • systemd-run
  • systemd-socket-activate
  • systemd-stdio-bridge
  • systemd-umount
  • timedatectl
ps -ef | grep -E "systemd|systemd|dns|dhcp|udevd" | grep -Fv grep
root         333       1  0 10:00 ?        00:00:05 /lib/systemd/systemd-journald
root         419       1  0 10:00 ?        00:00:01 /lib/systemd/systemd-udevd
systemd+     546       1  0 10:00 ?        00:00:07 /lib/systemd/systemd-oomd
systemd+     547       1  0 10:00 ?        00:00:00 /lib/systemd/systemd-resolved
systemd+     548       1  0 10:00 ?        00:00:00 /lib/systemd/systemd-timesyncd
message+     573       1  0 10:00 ?        00:00:04 @dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
root         680       1  0 10:00 ?        00:00:00 /lib/systemd/systemd-logind
root         682       1  0 10:00 ?        00:00:00 /usr/sbin/thermald --systemd --dbus-enable --adaptive
markjour     2508       1  0 10:05 ?        00:00:01 /lib/systemd/systemd --user
markjour     2523    2508  0 10:05 ?        00:00:00 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
markjour     5461    2508  0 10:11 ?        00:00:00 /usr/libexec/gnome-session-binary --systemd-service --session=ubuntu
root        6630       1  0 10:15 ?        00:00:00 systemd-tty-ask-password-agent --wall

#346 Systemd 子命令

2019-12-21
Unit Commands:
  list-units [PATTERN...]             List units currently in memory
  list-sockets [PATTERN...]           List socket units currently in memory,
                                      ordered by address
  list-timers [PATTERN...]            List timer units currently in memory,
                                      ordered by next elapse
  is-active PATTERN...                Check whether units are active
  is-failed PATTERN...                Check whether units are failed
  status [PATTERN...|PID...]          Show runtime status of one or more units
  show [PATTERN...|JOB...]            Show properties of one or more
                                      units/jobs or the manager
  cat PATTERN...                      Show files and drop-ins of specified units
  help PATTERN...|PID...              Show manual for one or more units
  list-dependencies [UNIT...]         Recursively show units which are required
                                      or wanted by the units or by which those
                                      units are required or wanted
  start UNIT...                       Start (activate) one or more units
  stop UNIT...                        Stop (deactivate) one or more units
  reload UNIT...                      Reload one or more units
  restart UNIT...                     Start or restart one or more units
  try-restart UNIT...                 Restart one or more units if active
  reload-or-restart UNIT...           Reload one or more units if possible,
                                      otherwise start or restart
  try-reload-or-restart UNIT...       If active, reload one or more units,
                                      if supported, otherwise restart
  isolate UNIT                        Start one unit and stop all others
  kill UNIT...                        Send signal to processes of a unit
  clean UNIT...                       Clean runtime, cache, state, logs or
                                      configuration of unit
  freeze PATTERN...                   Freeze execution of unit processes
  thaw PATTERN...                     Resume execution of a frozen unit
  set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit
  bind UNIT PATH [PATH]               Bind-mount a path from the host into a
                                      unit's namespace
  mount-image UNIT PATH [PATH [OPTS]] Mount an image from the host into a
                                      unit's namespace
  service-log-level SERVICE [LEVEL]   Get/set logging threshold for service
  service-log-target SERVICE [TARGET] Get/set logging target for service
  reset-failed [PATTERN...]           Reset failed state for all, one, or more
                                      units
Unit File Commands:
  list-unit-files [PATTERN...]        List installed unit files
  enable [UNIT...|PATH...]            Enable one or more unit files
  disable UNIT...                     Disable one or more unit files
  reenable UNIT...                    Reenable one or more unit files
  preset UNIT...                      Enable/disable one or more unit files
                                      based on preset configuration
  preset-all                          Enable/disable all unit files based on
                                      preset configuration
  is-enabled UNIT...                  Check whether unit files are enabled
  mask UNIT...                        Mask one or more units
  unmask UNIT...                      Unmask one or more units
  link PATH...                        Link one or more units files into
                                      the search path
  revert UNIT...                      Revert one or more unit files to vendor
                                      version
  add-wants TARGET UNIT...            Add 'Wants' dependency for the target
                                      on specified one or more units
  add-requires TARGET UNIT...         Add 'Requires' dependency for the target
                                      on specified one or more units
  edit UNIT...                        Edit one or more unit files
  get-default                         Get the name of the default target
  set-default TARGET                  Set the default target

Machine Commands:
  list-machines [PATTERN...]          List local containers and host

Job Commands:
  list-jobs [PATTERN...]              List jobs
  cancel [JOB...]                     Cancel all, one, or more jobs

Environment Commands:
  show-environment                    Dump environment
  set-environment VARIABLE=VALUE...   Set one or more environment variables
  unset-environment VARIABLE...       Unset one or more environment variables
  import-environment VARIABLE...      Import all or some environment variables

Manager State Commands:
  daemon-reload                       Reload systemd manager configuration
  daemon-reexec                       Reexecute systemd manager
  log-level [LEVEL]                   Get/set logging threshold for manager
  log-target [TARGET]                 Get/set logging target for manager
  service-watchdogs [BOOL]            Get/set service watchdog state

System Commands:
  is-system-running                   Check whether system is fully running
  default                             Enter system default mode
  rescue                              Enter system rescue mode
  emergency                           Enter system emergency mode
  halt                                Shut down and halt the system
  poweroff                            Shut down and power-off the system
  reboot                              Shut down and reboot the system
  kexec                               Shut down and reboot the system with kexec
  exit [EXIT_CODE]                    Request user instance or container exit
  switch-root ROOT [INIT]             Change to a different root file system
  suspend                             Suspend the system
  hibernate                           Hibernate the system
  hybrid-sleep                        Hibernate and suspend the system
  suspend-then-hibernate              Suspend the system, wake after a period of
                                      time, and hibernate