使用 Python3 在 AWS lambda 中进行多线程
·
问题:使用 Python3 在 AWS lambda 中进行多线程
我正在尝试在 AWS lambda 中实现多线程。这是一个示例代码,它定义了我试图在 lambda 中执行的原始代码的格式。
import threading
import time
def this_will_await(arg,arg2):
print("Hello User")
print(arg,arg2)
def this_should_start_then_wait():
print("This starts")
timer = threading.Timer(3.0, this_will_await,["b","a"])
timer.start()
print("This should execute")
this_should_start_then_wait()
在我的本地机器中,此代码运行良好。我收到的输出是:
This starts
This should execute
.
.
.
Hello User
('b', 'a')
那些 3 .表示等待3秒完成执行。
现在,当我在 AWS lambda 中执行相同的操作时。我只收到
This starts
This should execute
我认为它没有调用 this_will_await() 函数。
解答
你试过添加timer.join()吗?您需要加入 Timer 线程,否则 Lambda 环境将在父线程完成时终止该线程。
Lambda 函数中的此代码:
import threading
import time
def this_will_await(arg,arg2):
print("Hello User")
print(arg,arg2)
def this_should_start_then_wait():
print("This starts")
timer = threading.Timer(3.0, this_will_await,["b","a"])
timer.start()
timer.join()
print("This should execute")
this_should_start_then_wait()
def lambda_handler(event, context):
return this_should_start_then_wait()
产生这个输出:
This starts
Hello User
b a
This should execute
更多推荐

所有评论(0)