Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play: should you always use Action.async?

I'm a little confused about how Play! works in its controller layer. Some documentation states that Play! is always asynchronous and non-blocking. So I'm confused by Action vs Action.async. Is Action blocking? If so, and docs say that you should always stay asynchronous and non-blocking, then should you always use Action.async in all your controllers? Why would you choose not too? I'm sure there's something that I'm not understanding here, and any of your expertise would be greatly appreciated in understanding this.

like image 927
Eric Avatar asked Dec 13 '13 23:12

Eric


2 Answers

Play is fully asynchronous and non-blocking.

Most actions tend to require being executed inline and so Play conveniently reflects this. However if you have an async operation within your action (calling WS for example) then you use Action.async to pass back the future result.

like image 151
Christopher Hunt Avatar answered Oct 10 '22 23:10

Christopher Hunt


Action is blocking and it takes a function from Request=>Result, so you can do any blocking stuff in it. Action.async is non-blocking and requires a function from Request=>Future[Result] but here it is important to realize that only non-blocking stuff in it has matter, because if you block some where further it would be pointless. WS call mentioned by @Christopher Hunt is a good example because it gives you a Future[Response] that you can easily transform to Future[Result] what is expected by Action.async as a return type. But you are free to use scala.Future API for any stuff that might be async & non-blocking.

like image 25
arussinov Avatar answered Oct 11 '22 01:10

arussinov