开发者

how does await() work in play?

开发者 https://www.devze.com 2023-03-04 13:33 出处:网络
In the playframework\'s documentation here has been writ开发者_如何转开发ten: public static void loopWithoutBlocking() {

In the playframework's documentation here has been writ开发者_如何转开发ten:

public static void loopWithoutBlocking() {
    for(int i=0; i<=10; i++) { 
         Logger.info(i);
         await("1s");
    }
    renderText("Loop finished");
}

I do not really understand how exactly it works:

for example when we are calling the method in first request i = 1, then .. unlocking and waiting.. and new request goes to the method and then it starts form i=0 again? and when first request awake it will have i=1 or 0 or 2?

It does not use any stateful mechanism here? like storing i.. between request for example?


The way this work in Play 1.1, was to use the suspend function, which was replaced in Play 1.2 with await(). In 1.1, the suspend function did not start the process off at the same point, but simply recalled the method, with the same inputs after the process had "suspended" for the specified amount of time.

The reason for this, is to ensure that the Thread that is "sleeping" is not blocking other requests from being processed (try in Dev mode, where only 1 thread is running, and await for 10seconds, you can still send a second request, and it is processed). So, in Play1.1, you would have had to maintain state.

The difference in Play 1.2, and using await, is that the restarting of the method is done behind the scenes, and it restarts the method at the point it left off, so the state of the variables should also be preserved.

In your example (taken from the docs), it should simply loop from zero to 9, and at each point, wait for 1 second. If you are not experiencing this, then I believe it is a bug.

0

精彩评论

暂无评论...
验证码 换一张
取 消