Python code for DynamoDB query is working on v3.6

2019-07-28 05:54发布

问题:

I have a DynamoDB query with boto3 framework, which works on my local machine running Python 3.6, but not my server running Python 2.7.

The working code on my local machine:

dyndb = boto3.resource('dynamodb')
table = dyndb.Table('XXXXXXX')

response = table.query(
      IndexName = "XXX-XXX-index",
      ProjectionExpression = "AssessID,SNo,Details,Status,OTP",
      KeyConditionExpression = Key('OTP').eq(otp))

The code running on server...

    global user_otp

         dyndb = boto3.resource('dynamodb')
         table = dyndb.Table('XXXXXX')

         otp = int(user_otp)

         print("converting string otp to int otp") # it is printed on console
         response = table.query(
                 IndexName = "XXX-XXX-index",
                 ProjectionExpression = "AssessID,SNo,Details,Status,OTP",
                 KeyConditionExpression = Key('OTP').eq(otp) & Key('SNo').between(1,5))

         print ("response code is executing file") # it is not printed on console

When I print the output, the first print is shown but not the second print after the table query.

I am making this query on global index with OTP as partition key and SNo as sort key. I get results on my local machine with only the partition key, but not on my server, even using both the partition and sort key.

DynamoDB does not raise any exceptions. instead I am getting tornado websocket exception.

control coming to process and response function
user otp mentioned is  3086and its type <type 'int'>
converting string otp to int otp

ERROR:tornado.application:Exception in callback <functools.partial object at 0x7f33b6ce7890>
Traceback (most recent call last):
  File "/usr/lib64/python2.7/site-packages/tornado/ioloop.py", line 758, in _run_callback
    ret = callback()
  File "/usr/lib64/python2.7/site-packages/tornado/stack_context.py", line 300, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/lib64/python2.7/site-packages/tornado/ioloop.py", line 779, in _discard_future_result
    future.result()
  File "/usr/lib64/python2.7/site-packages/tornado/concurrent.py", line 261, in result
    raise_exc_info(self._exc_info)
  File "/usr/lib64/python2.7/site-packages/tornado/gen.py", line 1141, in run
    yielded = self.gen.throw(*exc_info)
  File "/usr/lib64/python2.7/site-packages/tornado/websocket.py", line 888, in _receive_frame_loop
    yield self._receive_frame()
  File "/usr/lib64/python2.7/site-packages/tornado/gen.py", line 1133, in run
    value = future.result()
  File "/usr/lib64/python2.7/site-packages/tornado/concurrent.py", line 261, in result
    raise_exc_info(self._exc_info)
  File "/usr/lib64/python2.7/site-packages/tornado/gen.py", line 1147, in run
    yielded = self.gen.send(value)
  File "/usr/lib64/python2.7/site-packages/tornado/websocket.py", line 975, in _receive_frame
    handled_future = self._handle_message(opcode, data)
  File "/usr/lib64/python2.7/site-packages/tornado/websocket.py", line 1000, in _handle_message
    return self._run_callback(self.handler.on_message, decoded)
  File "/usr/lib64/python2.7/site-packages/tornado/websocket.py", line 548, in _run_callback
    self.handler.log_exception(*sys.exc_info())
AttributeError: 'WebSocketClientConnection' object has no attribute 'log_exception'

回答1:

I think "no attribute log_exception" was a bug in Tornado 5 that was fixed in Tornado 6. However, Tornado 6 only supports Python 3, so in Python 2 you get the older version.

There's also another error here, but you can't see what it is because Python 2's error handling is not as good as Python 3's. And the error apparently doesn't occur on Python 3. If you must continue to support Python 2, try adding a try/except block around the body of your on_message callback (or use the read_message interface instead of the on_message callback).