How to instantiate a class just after starting Django server and access its members later in views.py

Viewed 112

I want to define a class PacketCount in Django.

class PacketCount:
    def __init__():
        self.count_pkts = 0
    def count_pkts():
        #logic to count the number of packets

I want to instantiate the object of PacketClass and continuously run the function count_pkts() just after starting starting the Django server using the command python manage.py runserver.

views.py contains the following function:

def index(request):
    # access PacketCountObj.count_pkts
    # return HttpResponse to display the value of PacketCountObj.count_pkts

urls.py contains the following:

urlpatterns = [
    path('', views.index, name='index'),
]

I am unable to figure out a way to automatically instantiate PacketCount class, call PacketCountObj.count_pkts() (possibly using a thread) on starting the Django server and then access PacketCountObj.count_pkts inside the views.py.
I do not want to use databases, django sessions etc.

0 Answers
Related