【发布时间】:2018-01-16 12:56:07
【问题描述】:
我正在使用“python-lambda-local”在本地测试一个简单的 python 程序。但是低于例外,虽然它在 AWS 环境中运行时工作正常。
以下是代码。
import os
import json
def lambda_handler(event, context):
what_to_print = os.environ.get("what_to_print")
how_many_times = int(os.environ.get("how_many_times"))
# make sure what_to_print and how_many_times values exist
if what_to_print and how_many_times > 0:
for i in range(0, how_many_times):
# formatted string literals are new in Python 3.6
print(f"what_to_print: {what_to_print}.")
return what_to_print
return None
使用以下命令运行它 -
$python-lambda-local -f lambda_handler lambda_handler.py event.json
环境 - Windows10 蟒蛇 - 3.6 例外 -
[root — INFO — 2018–01–13 22:14:22,138] Event: {‘what_to_print1’: ‘Hello’, ‘how_many_times1’: ‘2’}
[root — INFO — 2018–01–13 22:14:22,138] START RequestId: 7c602d94–7fa4–45ae-91db-674877eba4ef
Process Process-1:
Traceback (most recent call last):
File “c:\program files\python36\lib\site-packages\lambda_local\main.py”, line 91, in execute
with time_limit(context.timeout):
File “c:\program files\python36\lib\contextlib.py”, line 82, in __enter__
return next(self.gen)
File “c:\program files\python36\lib\site-packages\lambda_local\timeout.py”, line 18, in time_limit
signal.signal(signal.SIGALRM, signal_handler)
AttributeError: module ‘signal’ has no attribute ‘SIGALRM’
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “c:\program files\python36\lib\multiprocessing\process.py”, line 249, in _bootstrap
self.run()
File “c:\program files\python36\lib\multiprocessing\process.py”, line 93, in run
self._target(*self._args, **self._kwargs)
File “c:\program files\python36\lib\site-packages\lambda_local\main.py”, line 53, in run
result, err_type = execute(func, e, c)
File “c:\program files\python36\lib\site-packages\lambda_local\main.py”, line 102, in execute
}, indent=4, separators=(‘,’, ‘: ‘))
File “c:\program files\python36\lib\json\__init__.py”, line 238, in dumps
**kw).encode(obj)
File “c:\program files\python36\lib\json\encoder.py”, line 201, in encode
chunks = list(chunks)
File “c:\program files\python36\lib\json\encoder.py”, line 430, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File “c:\program files\python36\lib\json\encoder.py”, line 404, in _iterencode_dict
yield from chunks
File “c:\program files\python36\lib\json\encoder.py”, line 325, in _iterencode_list
yield from chunks
File “c:\program files\python36\lib\json\encoder.py”, line 437, in _iterencode
o = _default(o)
File “c:\program files\python36\lib\json\encoder.py”, line 180, in default
o.__class__.__name__)
TypeError: Object of type ‘FrameSummary’ is not JSON serializable
Process finished with exit code 1
【问题讨论】:
-
不太确定。但这可能是因为Windows没有
SIGALRM。 -
您的活动看起来不正确。在 JSON 中,您应该使用
"而不是'。 -
@KrishnaKumarR - 我在 json 中使用 "。
$ cat event.json { "what_to_print": "Hello", "how_many_times": "2" } -
你在读取文件时是否使用
json.load加载了json? -
我已经添加了代码和执行命令。我已经使用 json.load 尝试过,但无法修复。
标签: python-3.x amazon-web-services aws-lambda