open-uri 打开命令在域上运行,针对同一域
我在网页上构建了一个表单,允许用户输入 URL,并返回有关该 URL CSS 的一些信息。除了我注意到的一个问题之外,该工具工作正常。
当我输入托管脚本的站点的 URL(在本地、临时服务器和生产服务器上测试)时,open-uri 命令“open”返回 Timeout::Error。现在这并不令人感到非常惊讶,我猜想有些东西被锁定在某处,并且在运行脚本和打开当前 URL 的过程中,进程被束缚了一点。
作为参考,这里是发生超时的方法:
# loads the Nokogiri XML object if it hasn't already been loaded
def site
begin
timeout(10) do
@site ||= Nokogiri::HTML(open(url))
end
rescue Timeout::Error
return nil
end
end
我的问题是,如何使脚本能够“打开”脚本当前正在运行的域?我是否需要创建另一个线程或其他东西来处理我的工具的这个特定方面,如果是这样,我如何确保我的脚本的其余部分不会运行,直到我从“打开”命令收到有效的内容?
感谢您对此的任何建议或提示。
Ive built a form on a webpage that allows a user to enter a URL and some information about the URL's CSS is returned. The tool works fine, apart from one issue i have noticed.
When i enter the URL of the site where the script is hosted (tested locally, on a staging server and on a production server), the open-uri command 'open' returns a Timeout::Error. Now this isn't hugely surprising, i'm guessing something is getting locked up somewhere along the line and in the process of running the script and opening the current URL, processes are getting tied up a little.
For reference, here is the method where the Timeout occurs:
# loads the Nokogiri XML object if it hasn't already been loaded
def site
begin
timeout(10) do
@site ||= Nokogiri::HTML(open(url))
end
rescue Timeout::Error
return nil
end
end
My question is, how do i enable the script to be able to 'open' the domain the script is currently running on? Do i need to create another Thread or something to process this particular aspect of my tool, if so how do i ensure the rest of my script isn't run until i receive something valid from the 'open' command?
Thanks for any suggestions or tips on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想知道你是否陷入了递归循环。您是否在 httpd 日志中看到重复的条目显示对同一站点的请求?
如果是这样,您可能希望将请求短路到该特定域并预先填充
@site
实例变量,以便它失败。I wonder if you're getting into a recursive loop. Are you seeing repeated entries in your httpd log showing requests for the same site?
If so you might want to short-circuit requests to that particular domain and pre-stuff the
@site
instance variable so it falls through.