簡體   English   中英

Play Framework是否適合異步后台處理?

[英]Is Play Framework suitable for asynchronous background processing?

我打算建立一個即將舉辦城市游戲的網絡應用程序。

用戶訪問我的網站,點擊“開始游戲”並在到達某個位置時開始接收一些短信,並且必須回答它們才能獲得積分。

Play適合這種應用嗎? 點擊“開始游戲”按鈕后,一些邏輯必須按照自己的方式進行。 我將如何平行地檢查玩家的地理定位(我有API)? 我想每隔約5秒鍾對播放器進行一次ping操作。 並做一些邏輯。 當然,用戶必須能夠在處理他的位置,分配點,發送和接收消息等的同時使用Web應用程序。

總而言之:我想要一個用Play編寫的應用程序,在點擊“開始游戲”之后為游戲啟動一個單獨的線程,而其他用戶可以查看他們的數據(statisctics等),同時線程在游戲中運行邏輯。

我找到了類似工作的東西,但它們已經在1.2版本中記錄下來 經過一番閱讀后發現Akka現在是推薦的,但它使用的是演員模型。

Play + Akka是我項目的不錯選擇嗎?

絕對。 使用Play Framework在單獨的ThreadPool(也稱為ExecutionContext)中設置計算非常容易。 您可能想要閱讀這里文檔 ,但簡而言之,您將希望在Application.scala控制器文件中執行類似的操作(請注意,此示例使用Scala):

  // Async Action that's triggered when a user clicks "Start Game".
  // Runs logic in separate gameLogicContext thread pool and asynchronously returns a response without blocking of Play's default thread pool.
  def startGame = Action.async { implicit request =>

    Future {

      // ... your game logic here. This will be run in gameLogicContext

      Ok("Game started in separate thread pool") // http response

    }(Contexts.gameLogicContext) // the thread pool the future should run in.

  }

然后,您將在application.conf文件中設置一個單獨的gameLogicContext線程池:

play {
  akka {
    actor {
      game-logic-context = {
        fork-join-executor {
          parallelism-min = 300
          parallelism-max = 300 // thread pool with 300 threads
        }
      }
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM