Monday, September 30, 2019

Re: Stop executing certain py files or delay in loading python files until Django server is up and runnning

Thanks Deep

On Mon, Sep 30, 2019 at 8:12 PM Deep Sukhwani <deepsukhwani@gmail.com> wrote:
What you are looking for is readiness check. Basically readiness is a concept wherein something reports to be ready to handle requests (this something could be a web server, a docker container, a kubernetes pod, a database, etc.)

You can custom define an API endpoint that returns 200 OK when your Django server is ready to handle the requests, and internally make a request to that API endpoint and only execute your file once the endpoint returns 200 OK.

Alternatively, there are third-party packages (a quick google search revealed django-probes) that might assist you in implementing something similar.

Logically, at a primitive level, your flow would be something along the lines of:
  • Define an API endpoint:
    • This should return a custom response WHILE it is YET to be ready to start taking requests
    • This should return a different custom response WHEN it is ready to start taking requests
  • Once you have the desired "ready" response, you go on invoking the file, until then you keep delaying it by adding a second's sleep or something.
Sample code

  1. import requests
  2.  
  3. """
  4. api_endpoint is the custom endpoint that returns your desired
  5. response based on whether or not Django Server is ready to take
  6. on requests
  7. """
  8. response = requests.get(api_endpoint)
  9.  
  10. while response.status_code != 200:
  11.     time.sleep(1) # Sleep for 1 second
  12.     response = requests.get(api_endpoint)
  13.  
  14. # Once this while loop breaks, this indicates we are good to proceed
  15.  
  16. if response.status_code == 200:
  17.     # Do rest of your job here

Pastebin link to above: https://pastebin.com/G3G41Xmb

--
Regards
Deep L Sukhwani


On Mon, 30 Sep 2019 at 16:12, Sampath Reddy <sampath.nandyala@gmail.com> wrote:
Hey All,

Using DRF for developing backend api end points for the application, I want to run a python module, only when we are sure Django server is up and running As I am calling the api end in that module.

So need to be sure Django server is up and running, before I can call the same server API end point.

Precisely What I am looking for is, how django loads files and starts executing them when I run "python manage.py runserver 0.0.0.0:8084" from command line. I want to delay some files execution if possible.

Any leads are much appreciated.

Thanks,
Sampath

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAErixQ3%3D_ms9y-RtHa89LMtgFvHBN2sQ32GJ1yef0MCtx7HT%3Dw%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEMqiPc_-kkbZbxiw9Ui7-NDb3kMb9Gv9vryoXh8wJLZkexHeA%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAErixQ2keY_N6CeD2tccq63eMpqKu2wGsVfd%2Bz8qx%3DswaEx-qg%40mail.gmail.com.

No comments:

Post a Comment