3

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.

abhi
  • 397
  • 3
  • 14
  • answer is here https://stackoverflow.com/questions/72615390/how-to-create-a-class-just-after-starting-django-server-and-access-its-members-l/72615511#72615511 – oruchkin Jun 14 '22 at 11:55

0 Answers0