pythondjangocommand-lineautorun

How can I jump to another line of a python code while the previous line is still running


I am trying to create a python script to auto-run all my Django commands, but the script execution stops at os.system('python manage.py runserver') and does not run the next line because os.system('python manage.py runserver') needs to keep running. How do I run the next line of code while os.system('python manage.py runserver') is still running?

I tried using the python sleep method to wait a few seconds and then run the next line, but it did not work.

Here is my code:

import os, webbrowser, time
os.system('pipenv shell')
os.system('python manage.py runserver')
time.sleep(5)
webbrowser.open('http://127.0.0.1:8000', new=1, autoraise=True)

The execution stops at os.system('python manage.py runserver') but I want it to run webbrowser.open('http://127.0.0.1:8000', new=1, autoraise=True) while os.system('python manage.py runserver') is still running.


Solution

  • Older modules os.system, os.spawn* are replaced by newer module with more functionalities subprocess and is recommended to use instead of older ones.

    Use os.spawnl(os.P_NOWAIT, 'python manage.py runserver') which will return the PID of the new process without waiting for the return code.

    subprocess.Popen() creates background child processes and does't wait to finish the child processes. For more please follow the documentation.