0

I'm having trouble getting the updated value of a class variable.

When ConnectTestAPI is called after the p_request function is executed, the class variables which are result and orderNo should updated in the post function.

Then I want to receive the updated value of class variables by looping while statement in the p_request function.

However, despite setting the values ​​of class variables with the post request, when the while statement is run, the corresponding values ​​are still empty and 0 value respectively,

So, the while statement cannot be terminated and results in a time out error. Here is my source code. Thank you in advance!

class ConnectTestAPI(APIView):
   result=""
   orderNo=0
   
   def post(self, request):
      data = request.data
      ConnectTestAPI.result = data['result'] 
      ConnectTestAPI.orderNo = data['orderNo'] 
      print(ConnectTestAPI.result) # I could successfully get data from POST request here!
      print(ConnectTestAPI.orderNo) # I could successfully get data from POST request here!
      return HttpResponse("ok")

   def p_request():
      data = {
         "a" : 1234,
         "b" : 5678
      }
      data = json.dumps(data,ensure_ascii=False).encode('utf-8')
      con = redis.StrictRedis(outside_server['ip'],outside_server['port'])
      con.set("data_dict", data)
      while True:
         if ConnectTestAPI.result != "" and ConnectTestAPI.orderNo != 0:
            break
      res_result = ConnectTestAPI.result
      res_orderNo = ConnectTestAPI.orderNo
      return res_result, res_orderNo
The_spider
  • 1,202
  • 1
  • 8
  • 18
Jo Jay
  • 123
  • 1
  • 3
  • 14

1 Answers1

0

You need to access the class variables using self:

class ConnectTestAPI(APIView):
   result=""
   orderNo=0
   
   def post(self, request):
      data = request.data
      self.result = data['result'] 
      self.orderNo = data['orderNo'] 
      print(self.result) # I could successfully get data from POST request here!
      print(self.orderNo) # I could successfully get data from POST request here!
      return HttpResponse("ok")

   def p_request():
      data = {
         "a" : 1234,
         "b" : 5678
      }
      data = json.dumps(data,ensure_ascii=False).encode('utf-8')
      con = redis.StrictRedis(outside_server['ip'],outside_server['port'])
      con.set("data_dict", data)
      while True:
         if self.result != "" and self.orderNo != 0:
            break
      res_result = self.result
      res_orderNo = self.orderNo
      return res_result, res_orderNo

NOTE: This usage of class attributes is not recommended. You are mutating a class attribute which has side effects on all instances of that class. In your case, an ordinary attribute initialized within __ init__() would be ok:

class ConnectTestAPI(APIView):
   def __init__(self):
      self.result=""
      self.orderNo=0
   
   def post(self, request):
      data = request.data
      self.result = data['result'] 
      self.orderNo = data['orderNo'] 
      print(self.result) # I could successfully get data from POST request here!
      print(self.orderNo) # I could successfully get data from POST request here!
      return HttpResponse("ok")

   def p_request():
      data = {
         "a" : 1234,
         "b" : 5678
      }
      data = json.dumps(data,ensure_ascii=False).encode('utf-8')
      con = redis.StrictRedis(outside_server['ip'],outside_server['port'])
      con.set("data_dict", data)
      while True:
         if self.result != "" and self.orderNo != 0:
            break
      res_result = self.result
      res_orderNo = self.orderNo
      return res_result, res_orderNo
Nechoj
  • 1,512
  • 1
  • 8
  • 18
  • Thank you for your answer!! I just tried both answers you uploaded, but still "self.result" and "self.orderNo" look the initial values... How can I solve? – Jo Jay Dec 01 '21 at 01:32
  • You need to show the code where you instantiate and use the class, I think. The class as defined above is ok. – Nechoj Dec 01 '21 at 09:26