为什么 time.sleep() 会破坏 Python 中的 syslog-ng

发布于 2024-12-29 07:04:30 字数 756 浏览 3 评论 0原文

我一直在尝试使用 syslog-ng 记录脚本输出,但是,如果我使用我的代码中的任何 time.sleep() 函数都会破坏 syslog-ng 并停止记录脚本的输出。

以下是详细信息。

// samplescript.py
import time

while True:
    print "hello world"
    time.sleep(5)

我使用管道将其输出发送到 syslog-ng,并使用 unix logger 工具,所以我这样称呼脚本;

$ python sampleoutput.py | logger

这不会向我的日志文件生成任何输出。代码很简单,而且可以工作。

顺便一提, 我对 syslog-ng conf 文件没有任何问题,因为如果我使用下面的代码,它会按预期工作。

// samplescript.py    
while True:
    print "hello world"

问:为什么 time.sleep() 会破坏 syslog-ng?我可以在代码中使用 sleep() 函数吗?

提前致谢。

I have been trying to log a script output with syslog-ng, however, if I use any time.sleep() function in my code, it breaks syslog-ng and stops logging the output of the script.

Here are the details.

// samplescript.py
import time

while True:
    print "hello world"
    time.sleep(5)

I use pipe to get it's output to syslog-ng, and I use unix logger tool, so I'm calling the script like this;

$ python sampleoutput.py | logger

This is not generating any output to my log file. The code is simple, and working.

By the way,
I don't thing anything wrong with syslog-ng conf file, since if i use the code below, it works as expected.

// samplescript.py    
while True:
    print "hello world"

Q: why time.sleep() is breaking syslog-ng? Is there any equivalence for sleep() function that I might use on my code?

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2025-01-05 07:04:30

您需要刷新缓冲的标准输出流:

import sys
sys.stdout.flush()

如何刷新 Python 打印的输出?

You need to flush the buffered stdout-stream:

import sys
sys.stdout.flush()

How to flush output of Python print?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文