My question is very similar with this one. The difference in my case is that the python script I want to start as a subprocess (listener1.py
), is part of my project, just on a different app.
This is my project structure:
my_project
├── my_app1 #This app contains models,views etc.
│ ├── models.py
│ └── apps.py
│ └── ...
├── logic_app #This app contains several unrelated python scripts
│ └── ManagerScript.py
│ └── listener1.py
│ └── ...
In my apps.py
:
class MyAppConfig(AppConfig):
name = 'my_app1'
def ready(self):
#Once app is ready, start the following script as a thread
thread = Thread(target=ManagerScript.main)
thread.start()
In ManagerScript.py
:
#This thread is responsible for starting listener1.py as a Subprocess
#(because this is a thread I can successfully import my Models here)
#from my_app1 import models #This would work!
tmp = ['python3','path/to/listener1.py']
Popen(tmp, preexec_fn=os.setpgrp)
In listener1.py
:
from my_app1 import models #Does not work!
...
My question is, considering the fact that the subprocess listener1.py
is in logic_app
folder, how could I import my Models
in listener1.py
script without using Django Management Commands? (Because using them would require to change my current project structure)