每个请求都会启动一个新的 sinatra 实例吗?
当一个新的http请求进来时,是否会启动一个新的sinatra实例,例如是否要初始化sinatra,或者只是调用sinatra的前一个实例的方法(相应的get/post方法/路由)? 感谢您提供任何文档链接,我找不到任何文档链接。
如果该行为取决于部署类型 - WEBrick/Passenger 等,那也会很有趣
When a new http request comes in, will a new instance of sinatra be started, e.g. has sinatra to be initialized, or is just the method of a previous instance (the corresponding get/post method/route) of sinatra be called?
Thank you for any documentation links, i wasn't able to find any.
It would also be interesting if that behavior is dependent on the deployment type - WEBrick/Passenger etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为每个请求创建一个新类。然而,这并不是由 Rack 完成的。这是西纳特拉的一个特点。如果您想深入了解细节:该实例实际上并不是使用
Sinatra::Application.new
创建的,而是使用Sinatra::Application.prototype.dup
创建的,请参阅Sinatra::Base#call
代码。A new class is created for every request. However, this is not done by Rack. This is a feature of Sinatra. If you want to dig into the details: The instance is not actually created with
Sinatra::Application.new
but withSinatra::Application.prototype.dup
, seeSinatra::Base#call
for the code.您应该始终假设整个应用程序可以在您的请求之间重新启动。如果您正在运行应用程序的 16 个副本,该怎么办 - 用户“jane”对“/”的请求可能会出现在副本#2 上,然后当她访问“/signup”时,请求将达到#12(可能为此启动)事件)应用程序。因此,Sinatra 做什么并不重要(尽管看起来他们做了类似的事情),因为你的应用程序可能会出现在任何地方,今天启动、昨天启动或几毫秒前启动。
如果您计划增长 - 或依赖 Heroku 等 - 您的应用程序需要使用“shotgun”良好运行 - 它会为每个请求重新启动所有内容。我想如果您的应用程序执行的操作与提供网页完全不同,并且几乎不会崩溃或重新启动,那么您可能会选择“否”,
所以我的答案是“是”(但并非总是如此,甚至有时通常也不会)。
尽管如此,了解事情是如何工作的还是很方便的,因此您也许只能在每次应用程序加载时设置一些复杂的计算资源缓存方案 - 这是一种性能选择。例如,如果每次使用 url /calculate_pi?decimals=2000 调用您的应用程序总是产生相同的 2000 位数字,您可以在每个实例上缓存该数字。
You should always assume that the entire app could be rebooted under you in-between requests. What if you are running 16 copies of your app - the request from user 'jane' for '/' could com e in on copy #2, then when she visits '/signup' the request will hit #12 (possibly booted for this event) app. So it does not matter what Sinatra does (although it looks like they do something similar), since your app could just appear anywhere, booted today, yesterday or a ms ago.
If you plan on growing - or deplying on Heroku, etc - your app needs to run fine using 'shotgun' - which restarts everything for each request. I guess if your app does something radically different than serve web pages, and hardly ever crashes or gets rebooted, you might get away with 'NO'
So my answer is 'YES' (but not always, and not even sometimes usually).
Nevertheless, it's handy to know how things work, so that you can perhaps only set up some complex calculated asset caching scheme once per app load - which is a performance opt. For example, if each call to your app with the url /calculate_pi?decimals=2000 always results in the same 2000 digit number, you could cache that on each instance.
快速测试表明,无论请求如何,同一个实例都在运行(至少在默认情况下)。
当这段代码运行并收到两个请求时,服务器会提示
flag is unset
,然后flag is unset
。编辑:
这表明文件没有重新加载。通过使用 puts self.object_id, self.class(如 pguardiario 推荐的),我们实际上看到为每个请求创建了一个新的
Sinatra::Application
实例。A quick test shows that the same instance is running whatever the request (at least, by default).
When this code is run and two requests are received, the server will prompt
flag is unset
and thenflag is unset
.EDIT:
That shows the files are not reloaded. By using
puts self.object_id, self.class
(as pguardiario recommended), we actually see that a new instance ofSinatra::Application
is created for each request.运行这个,你就会知道一切,但这并不意味着 Rack 机制像 Sinatra 一样运行。(实际上,Rack 会为每个请求创建一个新实例)
Run this, you will know everything, but it dosen't mean the Rack mechanical of running as the Sinatra.(Actually, the Rack will creates a new instance for every request)