简体   繁体   English

AWS Lambda Java多线程

[英]AWS Lambda Java Multithreading

Hello I have made an AWS Lambda function for Kinesis stream for batch size of 100 & I am trying to execute it in multi-threaded environment but the problem is that in multi-threaded environment,it works very slow compare to single-threaded .. Like I can share you the numbers : For 512 MB with 60 seconds timeout,Lambda function executes 955 records in 684 ms & 1031 records in 435 ms & For multithreaded it executes 430 records in 878808 ms & 433 records in 893862 ms for same memory (ie 512 MB & 60 second timeout) 您好我为Kinesis流制作了一个AWS Lambda函数,批量大小为100,我试图在多线程环境中执行它,但问题是在多线程环境中,与单线程相比,它的工作速度非常慢。就像我可以分享你的数字:对于512 MB,60秒超时,Lambda函数在684 ms内执行955条记录,在435 ms内执行1031条记录。对于多线程,它在878808 ms中执行430条记录,在893862 ms内执行相同内存的433条记录(即512 MB和60秒超时)

Following is my Lambda function code which is executing in multi-threaded behaviour: 以下是我的Lambda函数代码,它以多线程行为执行:

public String myHandler(final KinesisEvent kinesisEvent, final Context context) {
      Thread thread = new Thread(new Runnable(){

            //@Override
            public void run() {
                int singleRecord=0;
                long starttime=System.currentTimeMillis();
                //LambdaLogger lambdaLogger=context.getLogger();
                for(KinesisEventRecord rec : kinesisEvent.getRecords())
                {
                    singleRecord=0;
                    System.out.println("Kinesis Record inside is:"+new String(rec.getKinesis().getData().array()));
                    //count++;
                    singleRecord++;
                    //  System.out.println(new String(rec.getKinesis().getData().array()));
                }
                count=count+singleRecord;
                long endtime=System.currentTimeMillis();
                long totaltime = endtime-starttime;
                time=time+totaltime;
                System.out.println("Time required to execute single Lambda function for "+singleRecord+" records is"+" :: "+totaltime+" milliseconds");
                System.out.println("Total time required to execute Lambda function for "+count+" records is"+" :: "+time+" milliseconds");
            }
        });
        thread.start();
        return null;
    } //end of handler method

Does Lambda function executes slow in multithreaded environment? Lambda函数在多线程环境中执行速度慢吗? I want to know what might be the reason behind the slow processing of this multithreaded Lambda function? 我想知道这个多线程Lambda函数处理速度慢的原因是什么? If I want this function to work faster than single-threaded function then what changes should I do in this code? 如果我希望这个函数比单线程函数更快地工作,那么我应该在这段代码中做些什么改变?

Two further options: 另外两个选择:

  1. Have your initial Lambda use the SDK to asynchronously launch further lambdas that actually do work (fan out) 让您的初始Lambda使用SDK异步启动实际工作的其他lambda(扇出)
  2. Use Lambda scheduling with scheduling offsets and long timeouts, so that you have multiple lambdas running concurrently. 将Lambda调度与调度偏移和长时间超时配合使用,以便同时运行多个lambda。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM