python - Python 原子日志
问题描述
我有一个在几千个进程上运行的记录器,它们都以附加模式写入同一个文件。什么是保证写入是原子性的好方法——也就是说,每次进程写入日志时,它的全部内容都写入一个块中,并且没有其他进程同时写入该文件?
我的想法是做类似的事情:
logger = getLogger()
global_lockfile = '/tmp/loglock'
def atomic_log(msg):
while True:
if os.path.exists(lockfile):
continue
with open(lockfile, 'w') as f:
logger.write(msg)
os.remove(lockfile)
def some_function(request):
log_atomic("Hello")
在 posix 系统上执行上述操作的实际方法是什么?
解决方案
推荐阅读
- c# - Net Core Web API 2.2 保护敏感数据
- sql - SQL + Powershell:当WHERE匹配时选择语句对匹配的行中的列执行更新
- c# - 我的请求在 HTTPClient 和 RestSharp 中不起作用,但在 Postman 中却很好
- django - 同一个表但不同列的多个外键获取models.object
- c++ - C++ 编译器为什么/如何理解类实例的这种初始化?
- sql-server - 使用 Row_number() 和 Partition,然后排序 desc,并在 DB2 中选择顶部结果
- android - 如何使用 AnimatedVectorDrawables 为渐变设置动画?
- r - 将 ggsave 中的画布大小设置为固定宽度,同时保持绘图的纵横比
- java - 仅在横向模式下强制旋转屏幕
- node.js - DA Node Bindings Tutorial -- 'retrieve the package identifiers' 问题