带有python asyncio的AWS lambda.事件循环关闭问题? [英] AWS lambda with python asyncio. Event loop closed problem?

查看:127
本文介绍了带有python asyncio的AWS lambda.事件循环关闭问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我在aws lambda服务中运行了一些aysncio python代码.代码的逻辑如下

  • I have some aysncio python code running within an aws lambda service. The logic of the code is as follows

def lambda_handler(event,context):
    loop = asyncio.get_event_loop()
    # perform all operations with the loop
    loop.close()
    return results

  • 如果我运行一次,它似乎可以正常工作.但是,如果我随后立即重新运行它,则会收到错误消息,提示事件循环已关闭

    推荐答案

    毕竟lambda应该是无状态

    您的功能应该是无状态的.

    来自 https://aws.amazon.com/lambda/faqs/:

    问:什么是AWS Lambda函数?

    ...

    代码必须以无状态"样式编写,即应假定与基础计算基础架构没有亲和力.

    The code must be written in a "stateless" style i.e. it should assume there is no affinity to the underlying compute infrastructure.

    ...

    问:AWS Lambda会重用函数实例吗?

    为了提高性能,AWS Lambda可以选择保留您的函数实例,然后将其重用于服务后续请求,而不是创建新副本.要了解有关Lambda如何重用函数实例的更多信息,请访问我们的文档.您的代码不应假定这种情况会一直发生.

    To improve performance, AWS Lambda may choose to retain an instance of your function and reuse it to serve a subsequent request, rather than creating a new copy. To learn more about how Lambda reuses function instances, visit our documentation. Your code should not assume that this will always happen.

    由于性能原因,当前的python实例已被重用,但绝不能依赖其重用或不重用.因此,尽管AWS Lambda本身并非总是无状态的,但您的编程方法应该是无状态的.希望这可以消除您对为什么发生的困惑!

    The current python instance is reused for performance reasons, but its reuse or non-reuse must never be relied on. So while AWS Lambda is not itself always stateless, your programming methodology should be. Hopefully that clears up your confusion as to why that is happening!

    这篇关于带有python asyncio的AWS lambda.事件循环关闭问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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