Hi, I’m trying to build a Django application that requires to spawn multiple threads other than the main one of the Django server itself.
However, I’m encountering this weird behaviour: Django is able to start one of the two threads that my application requires to use but when i add the other two therads in both apps.py and settings.py, I’m greeted with the following error code in the django console:
django.core.exceptions.ImproperlyConfigured: Cannot import ‘grafico’. Check that ‘pages.apps.InizializzaThreadGrafico.name’ is correct
I also tried to check for typos in the code but I didn’t find any typos in the code so far.
I also can’t use extrnal multitherading libraries such as celery due to project contraints
I put the code for both my apps.py (Thread declaration) and settings.py
I also put the stackoverflow limks that i have used to implement the multithread part with django
[Link 1] (python - How to start a background thread when django server is up? - Stack Overflow)
[Link 2] (python - How to avoid AppConfig.ready() method running twice in Django - Stack Overflow)
Thank you in advance for the support
apps.py
import os
from threading import Thread
from django.apps import AppConfig
# pages application declaration
class PagesConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'pages'
# filtra_dati_unico application declaration
class InizializzaThreadFiltraDati(AppConfig):
name = 'filtra_dati_unico'
def ready(self):
FiltraDatiThread.daemon = True
run_once = os.environ.get('CMDLINERUNNER_RUN_ONCE')
if run_once is not None:
return
os.environ['CMDLINERUNNER_RUN_ONCE'] = 'True'
FiltraDatiThread().start()
# Application declaration grafico_impianto
class InizializzaThreadGrafico(AppConfig):
name = 'grafico_impianto'
def ready(self):
GraficoThread.daemon = True
run_once = os.environ.get('CMDLINERUNNER_RUN_ONCE')
if run_once is not None:
return
os.environ['CMDLINERUNNER_RUN_ONCE'] = 'True'
GraficoThread().start()
# Thread FiltraDati declaration
class FiltraDatiThread(Thread):
def run(self):
print('Thread FiltraDati initialized')
# Thread GraficoThread Initialization
class GraficoThread(Thread):
def run(self):
print('Thread GraficoThread initialized')
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bootstrap5',
'pages.apps.PagesConfig',
# Custom app definition (Threads)
'pages.apps.InizializzaThreadFiltraDati',
'pages.apps.InizializzaThreadGrafico',
]