如何用Future [StandardRoute]完成请求

时间:2016-10-16 08:44:14

标签: scala akka-http

我的代码简化如下:

# Test data
df= DataFrame([True, True, False, False, False, False, True, False, False], 
              index=pd.to_datetime(['2015-05-01', '2015-05-02', '2015-05-03',
                                   '2015-05-04', '2015-05-05', '2015-05-06',
                                   '2015-05-07', '2015-05-08', '2015-05-09']), 
              columns=['A'])

# We have to ensure that the index is sorted
df.sort_index(inplace=True)
# Resetting the index to create a column
df.reset_index(inplace=True)

# Grouping by the cumsum and counting the number of dates and getting their min and max
df = df.groupby(df['A'].cumsum()).agg(
    {'index': ['count', 'min', 'max']})

# Removing useless column level
df.columns = df.columns.droplevel()

print(df)
#    count        min        max
# A                             
# 1      1 2015-05-01 2015-05-01
# 2      5 2015-05-02 2015-05-06
# 3      3 2015-05-07 2015-05-09

# Getting the max
df[df['count']==df['count'].max()]

#    count        min        max
# A                             
# 2      5 2015-05-02 2015-05-06

有一次,我的Future [StandardRoute]包含我的结果,但我不知道如何在不阻止Future的情况下完成此请求。

1 个答案:

答案 0 :(得分:3)

在处理期货onComplete时,您可以使用without blocking完成请求。 onComplete占用了未来,然后我们可以在successfailure上进行模式匹配,以准备Http响应。

path("path")  {
  post {
    val routeFuture: Future[StandardRoute] = Future {
      utilFunctionRoute()
    }

    onComplete(routeFuture){
      case util.Success(f) =>
        complete(StatusCodes.OK)

      case util.Failure(ex) =>
        complete(StatusCodes.InternalServerError )
  }
}