Python 打印到生成器中的 stderr
构建一个 python 保险丝 fs,在我的 readdir 生成器中,第一行代码是打印语句。这从未出现在我的控制台上。我将其修改为打印到 stderr,因为我认为这是一个缓冲问题。仍然没有输出。
我在下一行添加了手动刷新 - 仍然没有。
我在下一行添加了 time.sleep(3) ,程序确实休眠了。
def readdir(self, path, offset):
print >> sys.stderr, 'Text'
sys.stderr.flush()
time.sleep(3)
然后我继续用其他代码填充目录(yield fusion.Direntry) 我确实得到了输出,并且可以在终端中执行 ls 来查看已安装的熔丝目录的内容,但我想知道为什么打印命令在这一个生成器中不起作用。
更新
对于那些正在苦苦挣扎的人:
def readdir(self, path, offset):
print >> sys.stderr, 'Text'
sys.stderr.flush()
for o in os.listdir( "." + path ):
yield fuse.Direntry(o)
是代码。它会生成一个文件列表,我可以在其中移动。那很好。 问题是我从未在 STDOUT、STDERR 或任何地方看到“文本”出现。
我只是想问为什么这种情况只发生在这个生成器中。我可以将 print 放在熔丝代码的其他位置并得到很好的输出。
Building a python fuse fs, in my readdir generator the first line of code is a print statement. This never appeared on my console. I modified it to a print to stderr as i thought it was a buffering issue. Still no output.
I added a manual flush to the next line - still nothing.
I added a time.sleep(3) to the next line, the prog does indeed sleep.
def readdir(self, path, offset):
print >> sys.stderr, 'Text'
sys.stderr.flush()
time.sleep(3)
I then go on to populate the directory with other code (yield fuse.Direntry)
I do get output and can do an ls in the terminal to see the contents of my mounted fuse directory, but I want to know why the print command doesn't work in this one generator.
Update
For those that are struggling :
def readdir(self, path, offset):
print >> sys.stderr, 'Text'
sys.stderr.flush()
for o in os.listdir( "." + path ):
yield fuse.Direntry(o)
Is the code. It generates a file list and I can move around it. That is fine.
The problem is I never see 'Text' appear, not in STDOUT, STDERR, anywhere.
I was simply asking why this happens only in this generator. I can put print elsewhere in the fuse code and get output very well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 FUSE 代码在 FUSE 库的控制下运行,因此您无法确定
stdin
、stdout
和stderr
是您的文件中的代码。终端会话。很有可能使用备用
std*
流调用readdir
生成器。既然您说 FUSE 代码的其他部分
print
没有任何问题,您可能需要在所有print
语句前添加调试日志消息(sys 的值) .stdout.isatty()
甚至是repr(sys.stdout)
可能会很方便)来帮助您了解正在发生的事情。Your FUSE code runs under the control of FUSE library, so you can't be sure that
stdin
,stdout
andstderr
are the ones in your terminal session.It's quite possible that the
readdir
generator is called with alternatestd*
streams.Since you say that other parts of your FUSE code
print
without any issues, you might want to prefix allprint
statements with a debugging log message (the value ofsys.stdout.isatty()
or even therepr(sys.stdout)
could be handy) to help you understand what is going on.