Windows下如何共享日志文件?
我有几个不同的进程,我希望它们都记录到同一个文件。这些进程在Windows 7系统上运行。有些是 python 脚本,有些是 cmd 批处理文件。
在 Unix 下,你只需让每个人都以追加模式打开文件并写入即可。只要每个进程在单个消息中写入的字节数少于 PIPE_BUF
字节,就可以保证每个 write
调用不会与任何其他调用交错。
在 Windows 下有没有办法实现这一点?这种天真的类 Unix 方法失败了,因为默认情况下 Windows 不喜欢多个进程同时打开一个文件进行写入。
I have several different processes and I would like them to all log to the same file. These processes are running on a Windows 7 system. Some are python scripts and others are cmd
batch files.
Under Unix you'd just have everybody open the file in append mode and write away. As long as each process wrote less than PIPE_BUF
bytes in a single message, each write
call would be guaranteed to not interleave with any other.
Is there a way to make this happen under Windows? The naive Unix-like approach fails because Windows doesn't like more than one process having a file open for writing at a time by default.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以将多个批处理进程安全地写入单个日志文件。我对 Python 一无所知,但我想这个答案中的概念可以与 Python 集成。
Windows 最多允许一个进程在任何时间点打开一个特定文件进行写访问。这可用于实现基于文件的锁定机制,以保证事件在多个进程之间序列化。请参阅 https://stackoverflow.com/a/9048097/1012053 和 http://www.dostips.com/forum/viewtopic.php?p=12454 获取一些示例。
由于您要做的只是写入日志,因此您可以使用日志文件本身作为锁。日志操作封装在一个子例程中,该子例程尝试以附加模式打开日志文件。如果打开失败,例程将循环返回并重试。一旦打开成功,日志就会被写入然后关闭,并且例程返回给调用者。该例程执行传递给它的任何命令,并且例程中写入 stdout 的任何内容都将重定向到日志。
下面是一个测试批处理脚本,它创建 5 个子进程,每个子进程写入日志文件 20 次。写入是安全交错的。
以下输出表明每个进程的所有 20 次写入均成功。
您可以打开生成的“myLog.log”文件以查看写入是如何安全交错的。但输出太大,无法在此发布。
通过修改 :log 例程使其在失败时不会重试,很容易证明来自多个进程的同时写入可能会失败。
以下是“破坏” :log 例程后的一些示例结果
It is possible to have multiple batch processes safely write to a single log file. I know nothing about Python, but I imagine the concepts in this answer could be integrated with Python.
Windows allows at most one process to have a specific file open for write access at any point in time. This can be used to implement a file based lock mechanism that guarantees events are serialized across multiple processes. See https://stackoverflow.com/a/9048097/1012053 and http://www.dostips.com/forum/viewtopic.php?p=12454 for some examples.
Since all you are trying to do is write to a log, you can use the log file itself as the lock. The log operation is encapsulated in a subroutine that tries to open the log file in append mode. If the open fails, the routine loops back and tries again. Once the open is successful the log is written and then closed, and the routine returns to the caller. The routine executes whatever command is passed to it, and anything written to stdout within the routine is redirected to the log.
Here is a test batch script that creates 5 child processes that each write to the log file 20 times. The writes are safely interleaved.
Here is the output that demonstrates that all 20 writes were successful for each process
You can open the resulting "myLog.log" file to see how the writes have been safely interleaved. But the output is too large to post here.
It is easy to demonstrate that simultaneous writes from multiple processes can fail by modifying the :log routine so that it does not retry upon failure.
Here are some sample results after "breaking" the :log routine
你可以尝试一下这个Python模块:
http://pypi.python.org/pypi/ConcurrentLogHandler
它提供了一个直接替代RotatingFileHandler 允许多个进程同时记录到单个文件,而不会删除或破坏日志事件。
我没有使用过它,但我在阅读相关错误时发现了它(问题 4749 )在Python中。
如果您实现自己的代码而不是使用该模块来执行此操作,请确保您阅读了该错误!
您可以使用输出重定向 在 Windows 上就像在 Bash 中一样。将批处理文件的输出通过管道传输到通过 ConcurrentLogHandler 进行记录的 Python 脚本。
You can give this Python module a try:
http://pypi.python.org/pypi/ConcurrentLogHandler
It provides a drop-in replacement the
RotatingFileHandler
which allows multiple processes to concurrently log to a single file without dropping or clobbering log events.I haven't used it, but I found out about it while reading up on a related bug (Issue 4749) in Python.
If you implement your own code to do it instead of using that module, make sure you read up on the bug!
You can use output redirection on Windows like you do in Bash. Pipe the output of the batch files to a Python script that logs through the
ConcurrentLogHandler
.