为什么修改控制器后不需要重新启动rails? Python Web框架中是否存在此类问题?

时间:2022-06-30 11:56:48

Rails does not need to be restarted after I modify the codes in views and controllers. Do such features also exist in Python web framework?

修改视图和控制器中的代码后,不需要重新启动Rails。 Python Web框架中是否也存在此类功能?

I tried Flask and Pyramid before. I know they don't need to be restarted if I only changed the template, but have to be restarted if I changed the views(which are similar to controllers in rails). And the restart takes a few seconds to finish.

我之前尝试过Flask和Pyramid。我知道如果我只更改了模板,则不需要重新启动它们,但如果我更改了视图(类似于rails中的控制器),则必须重新启动。重启需要几秒钟才能完成。

As I remember, rails doesn't need to be restarted even when I changed codes in controllers. Does anyone have ideas about whether such feature also exists in Python web framework?

我记得,即使我在控制器中更改了代码,也不需要重新启动rails。有没有人有关于这个功能是否也存在于Python Web框架中的想法?

2 个解决方案

#1


2  

Rails implements development-time reloading as follows. There are two mechanisms at work.

Rails实现开发时重新加载如下。有两种机制在起作用。

  1. Whenever the code references a class that does not exist, a callback (const_missing) can be called. Rails installs such a callback. In this callback it checks whether there is a corresponding class in one of the application load paths (e.g. in app/models), loads it and carries on. This mechanism is called "Rails autoloading" (not to be confused to Ruby autoloading which is something different).
  2. 只要代码引用了不存在的类,就可以调用回调(const_missing)。 Rails安装了这样的回调。在此回调中,它检查其中一个应用程序加载路径中是否存在相应的类(例如,在app / models中),加载它并继续。这种机制称为“Rails自动加载”(不要混淆Ruby自动加载,这是不同的东西)。
  3. Rails tracks which classes have been autoloaded. At the end of the request, it removes all those classes from memory.
  4. Rails跟踪哪些类已自动加载。在请求结束时,它会从内存中删除所有这些类。

Obviously this has some limitations. If your code stores stuff in global variables then Rails will not clear them at the end of the request, so if you global variables keep growing you will leak memory during development. Reloading also causes subtle differences in semantics between development in production. Whereas something like

显然这有一些局限性。如果您的代码将内容存储在全局变量中,那么Rails将不会在请求结束时清除它们,因此如果全局变量不断增长,您将在开发过程中泄漏内存。重新加载还会导致生产中的开发之间的语义细微差异。而类似的东西

scope :older_than_one_year, where('date < ?', 1.year.ago)

works as intended in development (because the model is reloaded on each request, causing 1.year.ago to be constantly re-evaluated), it doesn't work as intended in production (because the 1.year.ago is only evaluated once, so if your app has been running for a year then that scope actually translates to 2 years ago).

在开发中按预期工作(因为模型在每个请求上重新加载,导致1.year.ago不断重新评估),它在生产中无法正常工作(因为1.year.ago仅评估一次,所以如果你的应用程序运行了一年,那么该范围实际上转换为2年前)。

I don't know whether something can be implemented in Python, I'm not familiar with Python's on-the-fly class creation/modification/removal capabilities or whether it has any hooks similar to const_missing.

我不知道是否可以用Python实现某些东西,我不熟悉Python的动态类创建/修改/删除功能,或者它是否有类似于const_missing的钩子。

#2


3  

Pyramid has an option to automatically restart the server after changing a file:

Pyramid可以选择在更改文件后自动重启服务器:

pserve development.ini --reload

pserve development.ini --reload

I'm sure each projects documentation provides an anser to your question

我确信每个项目文档都会为您提出问题

Django does as well (which you didn't ask for in your question)

Django也是如此(你在问题中没有要求)

#1


2  

Rails implements development-time reloading as follows. There are two mechanisms at work.

Rails实现开发时重新加载如下。有两种机制在起作用。

  1. Whenever the code references a class that does not exist, a callback (const_missing) can be called. Rails installs such a callback. In this callback it checks whether there is a corresponding class in one of the application load paths (e.g. in app/models), loads it and carries on. This mechanism is called "Rails autoloading" (not to be confused to Ruby autoloading which is something different).
  2. 只要代码引用了不存在的类,就可以调用回调(const_missing)。 Rails安装了这样的回调。在此回调中,它检查其中一个应用程序加载路径中是否存在相应的类(例如,在app / models中),加载它并继续。这种机制称为“Rails自动加载”(不要混淆Ruby自动加载,这是不同的东西)。
  3. Rails tracks which classes have been autoloaded. At the end of the request, it removes all those classes from memory.
  4. Rails跟踪哪些类已自动加载。在请求结束时,它会从内存中删除所有这些类。

Obviously this has some limitations. If your code stores stuff in global variables then Rails will not clear them at the end of the request, so if you global variables keep growing you will leak memory during development. Reloading also causes subtle differences in semantics between development in production. Whereas something like

显然这有一些局限性。如果您的代码将内容存储在全局变量中,那么Rails将不会在请求结束时清除它们,因此如果全局变量不断增长,您将在开发过程中泄漏内存。重新加载还会导致生产中的开发之间的语义细微差异。而类似的东西

scope :older_than_one_year, where('date < ?', 1.year.ago)

works as intended in development (because the model is reloaded on each request, causing 1.year.ago to be constantly re-evaluated), it doesn't work as intended in production (because the 1.year.ago is only evaluated once, so if your app has been running for a year then that scope actually translates to 2 years ago).

在开发中按预期工作(因为模型在每个请求上重新加载,导致1.year.ago不断重新评估),它在生产中无法正常工作(因为1.year.ago仅评估一次,所以如果你的应用程序运行了一年,那么该范围实际上转换为2年前)。

I don't know whether something can be implemented in Python, I'm not familiar with Python's on-the-fly class creation/modification/removal capabilities or whether it has any hooks similar to const_missing.

我不知道是否可以用Python实现某些东西,我不熟悉Python的动态类创建/修改/删除功能,或者它是否有类似于const_missing的钩子。

#2


3  

Pyramid has an option to automatically restart the server after changing a file:

Pyramid可以选择在更改文件后自动重启服务器:

pserve development.ini --reload

pserve development.ini --reload

I'm sure each projects documentation provides an anser to your question

我确信每个项目文档都会为您提出问题

Django does as well (which you didn't ask for in your question)

Django也是如此(你在问题中没有要求)