0

I am running a get request that is retrieving the status of a specific server process and I want to rerun this request within the script until the status returns finished so that I can execute follow-up functions in my script.

The code I have so far includes a function that does the get request to get the current status and a while loop that is supposed to rerun the function as long as it returns "PENDING" but its not working as intended.

def get_status(id, token):
    url = "exampleurl.com/status"
    headers = {'Authorization' : 'Bearer ' + token}
    response = requests.request("GET", url, headers=headers)
    resp_dict = json.loads(response.text)
    current_status = resp_dict['status']
    return current_status

 while status(id, key) == 'PENDING':
    status(id, key)
    if status(id, key) == 'FINISHED':
        print('its done')
   
Tim
  • 161
  • 7
  • 24
  • 1
    print(status) doesn't return status, it returns None – Christian Sloper Oct 04 '20 at 22:09
  • Also note that you're calling `status` *twice* per iteration, which means you're making *two requests per iteration*. – Carcigenicate Oct 04 '20 at 22:11
  • Possible weak duplicate of [this question](https://stackoverflow.com/questions/7664779/what-is-the-formal-difference-between-print-and-return)? – Carcigenicate Oct 04 '20 at 22:12
  • @Tim, i see a few challenges with your code. You have a function defined as `status`. you are also loading the json into a variable `status` and you are calling `status` from the print statement. What do you think `status` represents inside the `status` function. – Joe Ferndz Oct 04 '20 at 22:20
  • @JoeFerndz you are right the naming is definitley lazy, I will reword this quickly for better readability. For your understanding the initial response returns a dict looking like so {'id': '1234', 'status': 'pending'}. From this I am extracting the 'pending' part into the status variable, which then prints in the return statement for now. My goal is to use this status to decide, whether to check the status again or not until it says 'finished' and then run other functions – Tim Oct 04 '20 at 22:36
  • @Tim, thanks for that. Yes, I understood your code but using the same variable to do multiple things may not be the best thing. good to know you are fixing it. Also one recommendation is not to return print statement. It will always result in a None – Joe Ferndz Oct 04 '20 at 22:44
  • @JoeFerndz Thanks for that, fixed the print part as well. Do you by chance have any input for my while loop on how I can get it to act in the way I want it to, as mentioned in my initial question? – Tim Oct 04 '20 at 23:01
  • responded to that in the answer section. It is easier to write all of that in the answer section. – Joe Ferndz Oct 04 '20 at 23:02
  • Please don't edit questions to include fixes to one problem and then ask about the next. Keep in mind that Stack Overflow is **not a discussion forum**. – Karl Knechtel Aug 17 '22 at 05:12

1 Answers1

1

Instead of adding comments, I thought it will be best to use the answer section to fix the code.

First, thanks for making the changes to the code. It looks better now.

In your function definition, your return statement has print(current_status). When you return a print statement, it will always return None.

Change your code to:

return current_status

While you changed the function name, left the while loop calling the older function. You need to change that to get_status(id, key). Note that your while loop can be optimized by checking if get_status(id, key) != 'FINISHED'

The third problem with this code is that you are not changing the value of id and key. So how are you expecting the result to change. Won't it always be PENDING resulting in an infinite loop?

 while status(id, key) == 'PENDING':
    status(id, key)
    if status(id, key) == 'FINISHED':
        print('its done')

I recommend you to make some changes so you can get a better result.

You can do something like this:

 while True:
    #you need to have a way to get new id & key
    if get_status(id, key) == 'FINISHED': 
        print('its done')
        break

Alternate, you can do this:

id = xxx #whatever the value
key = yyy #whatever the value

while get_status(id, key) != 'FINISHED':
    #add code to change the value of id & key

This will keep calling the function with new values of id and key until it get a status result of 'FINISHED'

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
  • 1
    Thanks so much for this great input! – Tim Oct 04 '20 at 23:11
  • Regarding your comment on the need for a new id it is not the goal. The status request is supposed to be done on the same process, which the id is the identifier for. The goal with the while loop is to monitor that process via the status requests until the request returns "finished" as status. Therefore I tried using the while loop to rerun the status request until that status is reached. Hope that makes sense. – Tim Oct 04 '20 at 23:20
  • The only risk is that the code could go into infinite loop if you don't get a FINISHED response. You may want to add some kind of delay + a counter to try it a few times. Otherwise, you have a risky code that could get stuck. Look for an alternate way to solve this. – Joe Ferndz Oct 04 '20 at 23:28