[英]MultiThreading in AWS lambda using Python3
我正在嘗試在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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.