I have an image detector module, that takes about one minute to load. I would like to instantiate it once when the server starts, and use it in views. I know that i can run code when the server starts at urls.py
, so, i tried the following:
urls.py
from django.contrib import admin
from django.urls import include, path
from module import Module
urlpatterns = [
path('module/', include('project.urls')),
path('admin/', admin.site.urls),
]
module = Module()
views.py
from django.http import HttpResponse
from project.urls import module
def end_point(request):
module.do_stuff()
return HttpResponse("It works!")
This approach did not work, because i can not import any variables from this file. Besides that, if urls.py
die, i would get NameError: name 'module' is not defined
. I do not use data base, i only want a REST API to my module. I would like to use Djongo, because i will use it in other services in my project.
Summing up: i want a place to instantiate an object once when server starts, and be able to use my object in views.
Thanks!