DynamoDB查询的Python代码适用于v3.6,但不适用于python 2.7 [英] Python code for DynamoDB query is working on v3.6 but not working in python 2.7

查看:138
本文介绍了DynamoDB查询的Python代码适用于v3.6,但不适用于python 2.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有boto3框架的DynamoDB查询,该查询可在运行Python 3.6的本地计算机上运行,​​但不能在运行Python 2.7的服务器上运行。

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.

我正在使用 OTP 作为分区键并且 SNo 作为排序键。即使在使用分区键和排序键的情况下,我也只能在本地计算机上使用分区键获得结果,而在服务器上却没有。

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不会引发任何异常。相反,我得到龙卷风websocket异常。

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'


推荐答案

我认为无属性log_exception是Tornado 5中的一个错误,该错误已在Tornado 6中修复。但是,Tornado 6仅支持Python 3,因此在Python 2中,您可以获得较早的版本。

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.

这里还有另一个错误,但是您看不到它是什么,因为Python 2的错误处理不如Python 3更好。而且该错误显然不会在Python 3上发生。如果您必须继续支持Python 2,请尝试在<$ c $的主体周围添加 try / except 块。 c> on_message 回调(或使用 read_message 接口代替 on_message 回调)。

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).

这篇关于DynamoDB查询的Python代码适用于v3.6,但不适用于python 2.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆