简体   繁体   English

Django:在runserver之后运行一个脚本

[英]Django: Run a script right after runserver

Context: 语境:

I have a table on the database that uses values from an external database. 我在数据库上有一个表,它使用外部数据库中的值。 This external database updates its values periodically. 此外部数据库定期更新其值。

Problem: 问题:

In order to update my database everytime i start the server, I want to run a script right after the runserver. 为了在每次启动服务器时更新我的​​数据库,我想在runserver之后运行一个脚本。

Potential Solution: 潜在解决方案

I have seen that it is possible to run a script from a certain app, which is something I'm interested in. This is achievable by using the django-extensions : 我已经看到可以从某个应用程序运行脚本,这是我感兴趣的东西。这可以通过使用django-extensions

https://django-extensions.readthedocs.io/en/latest/runscript.html https://django-extensions.readthedocs.io/en/latest/runscript.html

However, this script only runs with the following command: 但是,此脚本仅使用以下命令运行:

python manage.py runscript your_script

Is there any other way to run a script from an app and execute it right after the runserver command? 有没有其他方法可以从应用程序运行脚本并在runserver命令后立即执行? I am open to suggestions! 我愿意接受建议!

Thanks in advance 提前致谢

Update 更新

Thanks to @Raydel Miranda for the remarks, I feel i left some information behind. 感谢@Raydel Miranda的评论,我觉得我留下了一些信息。

My goal is, once I start the server I'm planning to open a socket to maintain my database updated. 我的目标是,一旦我启动服务器,我打算打开一个套接字来维护我的数据库更新。

You can execute the code in the top-level urls.py . 您可以在顶级urls.py执行代码。 That module is imported and executed once. 该模块导入并执行一次。

urls.py

from django.confs.urls.defaults import *
from your_script import one_time_startup_function

urlpatterns = ...

one_time_startup_function()

I would recommend to use something like this, lets say you have the script like this: 我建议使用这样的东西,假设你有这样的脚本:

# abc.py

from your_app.models import do_something

do_something()

Now you can run this script right after runserver(or any other way you are running the django application) like this: 现在您可以在runserver之后运行此脚本(或运行django应用程序的任何其他方式),如下所示:

python manage.py runserver & python manage.py shell < abc.py

FYI, it will only work if you have bash in your terminal (like in ie Linux, MacOs). 仅供参考,只有你的终端有bash(例如Linux,MacOs)才会有效。

Update 更新

After reading you problem carefully, I think running a script after runserver might not be the best solution. 在仔细阅读了您的问题后,我认为在runserver之后运行脚本可能不是最佳解决方案。 As you said: 如你所说:

This external database updates its values periodically. 此外部数据库定期更新其值。

So, I think you need some sort of perodic task to do this update. 所以,我认为你需要某种perodic任务来做这个更新。 You can use cronjob or you can use Celery for this. 您可以使用cronjob ,也可以使用Celery

Running the script after runserver don't seem a very good idea, the main reason is that you will have a window since the server is running (and available for users) till you finish synchronizing your data. 在runserver之后运行脚本似乎不是一个好主意,主要原因是您将有一个窗口,因为服务器正在运行(并且可供用户使用),直到您完成同步数据。 Also if you synchronize using a script after runserver you won't get updates from the external db after that. 此外,如果您在runserver之后使用脚本进行同步,则之后将无法从外部数据库获取更新。

The best solution for this is to configure multiple databases, you can use the external database with only read access. 对此最好的解决方案是配置多个数据库,您可以使用仅具有读访问权限的外部数据库。 This way your views will provide really updated data. 这样您的视图将提供真正更新的数据。

On the other hand ... 另一方面 ...

If want use something like a script is better to write a Django custom command (this way you don't have to deal with initializing django settings and other issues) and execute it using cron or celery as @ruddra states in his/her answer. 如果想要使用类似脚本的东西,最好编写一个Django自定义命令 (这样你不必处理初始化django设置和其他问题)并使用cron或celery执行它,因为@ruddra在他/她的回答中说明。

Said this, you should see this: https://docs.djangoproject.com/en/2.1/topics/db/multi-db/ 说这个,你应该看到这个: https//docs.djangoproject.com/en/2.1/topics/db/multi-db/

This may help. 可能有所帮助。 you can edit yourapp/apps.py 你可以编辑yourapp / apps.py

class MyAppConfig(AppConfig):
    name = 'myapp'

    def ready(self):
        # update my database here
        pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM