How to add polling interval for a GET request in python

2024/9/20 17:25:38

I have a case where I have to keep checking the response of a GET call until I see the status as success in the api response. And it takes around 20 to 50 mins to get the status from active to success. Only when I see this status I can perform my other action.

How can I achieve that in python?

I tried this polling python library. But it's not much of a help.

This is my code. But it's not working how I want. Is there any other way to do this?

    try:while True:response = requests.get(f'{os.environ["BASE_URL"]}/syncs/076532', headers=headers)json_res = response.json()if json_res.get('status') != 'success':logging.info("Polling started.......")logging.info("Waiting.......")time.sleep(120)print("Got status as success. Proceeding......")sys.exit()except KeyboardInterrupt:logging.info("exiting")sys.exit()

Thanks in Advance

Answer

You can try this:

def custom_request(url: str, headers: dict):if requests.get(url=url, headers=headers).json().get('status') != 'success':logging.info("Polling started.......")logging.info("Waiting.......")return Falsereturn Truedef main():try:polling.poll(custom_request,step=60,args=(f'{os.environ["BASE_URL"]}/syncs/076532', headers),poll_forever=True)print("Got status as success. Proceeding......")exit()except KeyboardInterrupt:logging.info("exiting")exit()

What difficulties have you faced by using polling?

https://en.xdnf.cn/q/119465.html

Related Q&A

Total beginner wrote a tic tac toe game in Python and would like some feedback

Ive decided to learn Python about 2 weeks ago, been going through various books and videos, and Ive decided to try my hand at programming a tic tac toe game. I was somewhat successful (it doesnt recogn…

Separating tag attributes as a dictionary

My entry (The variable is of string type): <a href="https://wikipedia.org/" rel="nofollow ugc">wiki</a>My expected output: { href: https://wikipedia.org/, rel: nofollow …

How can I remove duplication of 2 separate which is interrelated with each other (PYTHON)

After reading so many title, I couldnt solved the problem below. Does anyone can help me please ? For instance, I have 2 list (list_I and list_II) which is interrelated with each other. list_I = [123,…

Array within an array?

Im trying to call up an element from an array within an array in Python. For example:array = [[a1,a2,a3,a4], [b1,b2,b3,b4], [c1,c2,c3,c4]]The question is, how would I print just the value b1?

How to create a zoned of gradation area on the edge of ROI in opencv python

I have a binary image (white and black), the where Region of Interest (ROI) is black. The shape of ROI is irregular and the location of ROI can be anywhere in the frame. I want to have a smooth gradati…

Prevent Terminal resize python curses

Im writing a program on python curses and I was wondering if there is a way to block terminal resizing in order to prevent curses crashing both on Linux and Windows. This is what happens.. Can I preven…

SymPy Not Doesnt Return LaTeX

Helloo! So, Im using SymPy to make a calculation for me. The trouble is, its output should be a LaTeX expression and in make case it prints something like SymPy Calculation Output Is there any way to s…

Python Flask: How to include JavaScript file for each template per blueprint

I have read Loading external script with jinja2 template directive and Import javascript files with jinja from static folder but unfortunately no closer I have a Python Flask site which is based on htt…

Difference between multiple elements in list with same string . Python 2.7 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…