gpt4 book ai didi

python - 读取 umask(线程安全)

转载 作者:太空狗 更新时间:2023-10-29 17:29:58 26 4
gpt4 key购买 nike

我知道在 Python 中读取 umask 的这种模式:

current_umask = os.umask(0)  # line1
os.umask(current_umask) # line2
return current_umask # line3

但这不是线程安全的。

在第 1 行和第 2 行之间执行的线程将具有不同的 umask。

是否有一种线程安全的方式来读取 Python 中的 umask?

相关:https://bugs.python.org/issue35275

最佳答案

如果您的系统在 /proc/[pid]/status 中有 Umask 字段,您可以从中读取:

import os

def getumask():
pid = os.getpid()
with open(f'/proc/{pid}/status') as f:
for l in f:
if l.startswith('Umask'):
return int(l.split()[1], base=8)
return None

在 CentOS 7.5、Debian 9.6 下测试。

或者,你可以添加一个线程锁:)

关于python - 读取 umask(线程安全),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53227072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com