Django: Run a script right after runserver

萝らか妹 提交于 2021-02-19 03:37:08

问题


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.

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:

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? I am open to suggestions!

Thanks in advance

Update

Thanks to @Raydel Miranda for the remarks, I feel i left some information behind.

My goal is, once I start the server I'm planning to open a socket to maintain my database updated.


回答1:


You can execute the code in the top-level 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()



回答2:


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:

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).

Update

After reading you problem carefully, I think running a script after runserver might not be the best solution. As you said:

This external database updates its values periodically.

So, I think you need some sort of perodic task to do this update. You can use cronjob or you can use Celery for this.




回答3:


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. Also if you synchronize using a script after runserver you won't get updates from the external db after that.

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.

Said this, you should see this: https://docs.djangoproject.com/en/2.1/topics/db/multi-db/




回答4:


This may help. you can edit yourapp/apps.py

class MyAppConfig(AppConfig):
    name = 'myapp'

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


来源:https://stackoverflow.com/questions/54229592/django-run-a-script-right-after-runserver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!