pythondjangodjango-commands

How to execute django runserver and python script at the same time (on one line in cmd)


I have wrote a python script which automatically uploads files from a given folder to a django modell. I want this script running while django server is running. My script runs perfectly when I run it in pycharm console.

I have tried a few command to achieve this:

python manage.py runserver & python manage.py shell < myscript.py
python manage.py runserver & python manage.py runscript -v3 myscript

In the first case nothing happens until I press ctrl+c, then it display errors "unexpected indent" for every single row of my code.

In the second case my script starts to run only when I press ctrl+c (so my django server is shut down and my script is executed).

I am using windows so celery is not a good alternative for me (and celery is overkill for such a simple task like that).


Solution

  • You should run python manage.py runserver in one terminal, and then run the other command in another terminal. In case you want to run both at the same time you could send runserver to the background with &, so for windows:

    Examples: Powershell equivalent of bash ampersand (&) for forking/running background processes

    From PowerShell Core 6.0, you can use the ampersen (&)

    python manage.py runserver &
    python manage.py runscript -v3 myscript
    

    Should do the job