我正在尝试编写一个ruby脚本来过滤尾文件(tail -f log.log | ./my_filter.rb)的输出。我相信我已经将stdin和stdout设置为同步读取,但是我仍然看到我的输出以延迟批处理的方式输出,一次输出20条左右,而不是实时输出。
我可以用简单的代码重现该问题:
#!/usr/bin/ruby
$stdout.sync = true
$stdin.sync = true
ARGF.each do |line|
puts line
end
Run Code Online (Sandbox Code Playgroud)
我是否缺少消除缓冲的设置?
编辑:澄清一下,如果我只是tail -f日志,那么我看到每秒写很多行。
所以我在函数中有以下几行代码
sock = urllib.urlopen(url)
html = sock.read()
sock.close()
Run Code Online (Sandbox Code Playgroud)
当我手动调用函数时,它们工作正常.但是,当我在循环中调用该函数(使用与之前相同的URL)时,我收到以下错误:
> Traceback (most recent call last):
File "./headlines.py", line 256, in <module>
main(argv[1:])
File "./headlines.py", line 37, in main
write_articles(headline, output_folder + "articles_" + term +"/")
File "./headlines.py", line 232, in write_articles
print get_blogs(headline, 5)
File "/Users/michaelnussbaum08/Documents/College/Sophmore_Year/Quarter_2/Innovation/Headlines/_code/get_content.py", line 41, in get_blogs
sock = urllib.urlopen(url)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib.py", line 87, in urlopen
return opener.open(url)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib.py", line 203, in open
return getattr(self, name)(url)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib.py", line 314, in open_http
if not host: raise …Run Code Online (Sandbox Code Playgroud) 我正在编写一个迭代向量的函数,Result如果它们都成功则返回成功,如果失败则返回错误.限制error::Error使我感到沮丧,我不知道如何解决它们.目前我有类似的东西:
let mut errors = Vec::new();
for result in results {
match result {
Err(err) => errors.push(err),
Ok(success) => { ... }
}
}
if errors.is_empty() {
return Ok(())
else {
return Err(MyErrorType(errors))
}
Run Code Online (Sandbox Code Playgroud)
我目前的做法的问题是,我只能设定一个错误是cause的MyErrorType,我的错误的description需求,是一个静态的String,所以我不能包括每个触发故障的说明.所有失败都可能与调用者有关.