简体   繁体   中英

Can I return 2 times in a def function?

I have a problem with my code.I'm working with API now, and when I call it, it will show me some result. And I want to show a loading text like this: "300: Calculating" to know that my code is processing, untill the result show out, the "300: Calculating " text will disappear. And my code is in a def function. My codeis roughly as below:

@api_view(['POST'])
def Measurementwarning_response():

 i = 0
while i < 3:
    if  i == 1:
        message_code = "300: Calculating"
        predict_response1 = {
            "message_code": message_code,
        }
        predict_response = predict_response1
        return HttpResponse(json.dumps(predict_response))
    

    if i == 2:
        message_code = "200. Sucessfully : Hello world"
        predict_response2 = {
            'message_code': message_code,

        }
        predict_response = predict_response2
        time.sleep(2)
        return HttpResponse(json.dumps(predict_response))
    i = i +1

I hope that after I call this API (Measurementwarning_response). The loading text "300: Calculating" will show out like this: This loading text "300: Calculating" will exisit whithin 2 seconds

End then, after 2 seconds, the result "200. Sucessfully: Hello world" will show out. And "300: Calculating" text will disappear like this This is the result will show out after 2 seconds and the "300: Calculating" text will disappear

I have been tried to make a while loop and return HttpResponse(json.dumps(predict_response)) 2 times. But it seems to be that I can not return 2 times in a function.I have been tried many times but I still can not do it. I hope you guys will take a look and comment in below to let me know what should I do with my code. Thank you guys so much

return statement ends the function. so you cannot return twice. You can use yield and generator function in python as an alternative.
The yield statement suspends a function's execution and sends a value back to the caller, but retains enough state to enable the function to resume where it left off. When the function resumes, it continues execution immediately after the last yield run. This allows its code to produce a series of values over time, rather than computing them at once and sending them back like a list.

def simpleGeneratorFun():
    yield 1
    yield 2
    yield 3
 
 
# Driver code to check above generator function
for value in simpleGeneratorFun():
    print(value)

Output:
1
2
3

@api_view(['POST'])
def Measurementwarning_response(request):
  idno = request.data['idno']
  source = request.data['source']
  measure_time = request.data['measure_time']
  exmtype = request.data['exmtype']
  code300 = "300: Calculating"
  code200 = "200: Successfully"
  predict_response1 = {"message_code": code300, }
  predict_response2 = {
    'message_code': code200,

  }
  yield predict_response1
  yield predict_response2

  for value in Measurementwarning_response():
     return HttpResponse(json.dumps(value))

@SaadRehman This is my code bases on yours. And I can not get any result...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM