作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我知道在 Python 中读取 umask 的这种模式:
current_umask = os.umask(0) # line1
os.umask(current_umask) # line2
return current_umask # line3
但这不是线程安全的。
在第 1 行和第 2 行之间执行的线程将具有不同的 umask。
是否有一种线程安全的方式来读取 Python 中的 umask?
最佳答案
如果您的系统在 /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/
我是一名优秀的程序员,十分优秀!