我想我可以说这个问题是由 lambda 在使用 Python 执行环境时的不同行为引起的。爪哇。
我有时间设置一个简单的 lambda 函数,并用 Java 和 Python 实现。该函数只是生成一个线程,在 while 循环中打印当前时间。问题是:即使 lambda 函数返回,Java 实现中的线程是否仍会继续打印,相反,Python 线程是否会停止?在这两种情况下答案都是肯定的:java 线程继续打印直到配置的超时,而 python 将在 lambda 函数返回后立即停止。
Java 版本的 CloudWatch 日志确认:
09:55:21 START RequestId: b70e732b-e476-11e6-b2bb-e11a0dd9b311 Version: $LATEST
09:55:21 Function started: 1485510921351
09:55:21 Pre function call: 1485510921351
09:55:21 Background function: 1485510921352
09:55:21 Background function: 1485510921452
09:55:21 Background function: 1485510921552
09:55:21 Background function: 1485510921652
09:55:21 Background function: 1485510921752
09:55:21 Post function call: 1485510921852
09:55:21 Background function: 1485510921853
09:55:21 END RequestId: b70e732b-e476-11e6-b2bb-e11a0dd9b311
09:55:21 REPORT RequestId: b70e732b-e476-11e6-b2bb-e11a0dd9b311 Duration: 523.74 ms Billed Duration: 600 ms Memory Size: 256 MB Max Memory Used: 31 MB
09:55:21 Background function: 1485510921953
09:55:22 Background function: 1485510922053
...
在 Python 版本中:
09:01:04 START RequestId: 21ccc71e-e46f-11e6-926b-6b46f85c9c69 Version: $LATEST
09:01:04 Function started: 2017-01-27 09:01:04.189819
09:01:04 Pre function call: 2017-01-27 09:01:04.189849
09:01:04 background_call function: 2017-01-27 09:01:04.194368
09:01:04 background_call function: 2017-01-27 09:01:04.294617
09:01:04 background_call function: 2017-01-27 09:01:04.394843
09:01:04 background_call function: 2017-01-27 09:01:04.495100
09:01:04 background_call function: 2017-01-27 09:01:04.595349
09:01:04 Post function call: 2017-01-27 09:01:04.690483
09:01:04 END RequestId: 21ccc71e-e46f-11e6-926b-6b46f85c9c69
09:01:04 REPORT RequestId: 21ccc71e-e46f-11e6-926b-6b46f85c9c69 Duration: 500.99 ms Billed Duration: 600 ms Memory Size: 128 MB Max Memory Used: 8 MB
这是两个函数的代码:
Python
import thread
import datetime
import time
def background_call():
while True:
print 'background_call function: %s' % (datetime.datetime.now(), )
time.sleep(0.1)
def lambda_handler(event, context):
print 'Function started: %s' % (datetime.datetime.now(), )
print 'Pre function call: %s' % (datetime.datetime.now(), )
thread.start_new_thread(background_call, ())
time.sleep(0.5)
print 'Post function call: %s' % (datetime.datetime.now(), )
return 'Needs more cowbell!'
Java
import com.amazonaws.services.lambda.runtime.*;
public class BackgroundTest implements RequestHandler<RequestClass, ResponseClass> {
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
public ResponseClass handleRequest(RequestClass requestClass, Context context) {
System.out.println("Function started: "+System.currentTimeMillis());
System.out.println("Pre function call: "+System.currentTimeMillis());
Runnable r = new Runnable() {
public void run() {
while(true){
try {
System.out.println("Background function: "+System.currentTimeMillis());
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread t = new Thread(r);
t.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Post function call: "+System.currentTimeMillis());
return new ResponseClass("Needs more cowbell!");
}
}