- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这可能是一 jar 蠕虫,我会尽力描述这个问题。我们有一个长期运行的数据处理工作。我们的行动数据库会每晚添加一次,并且会处理未完成的行动。处理每晚的操作大约需要15分钟。在Vapor 2中,我们利用了很多原始查询来创建PostgreSQL游标并循环遍历它,直到它为空。
目前,我们通过命令行参数运行该处理。将来,我们希望它作为主服务器的一部分运行,以便在执行处理时可以检查进度。
func run(using context: CommandContext) throws -> Future<Void> {
let table = "\"RecRegAction\""
let cursorName = "\"action_cursor\""
let chunkSize = 10_000
return context.container.withNewConnection(to: .psql) { connection in
return PostgreSQLDatabase.transactionExecute({ connection -> Future<Int> in
return connection.simpleQuery("DECLARE \(cursorName) CURSOR FOR SELECT * FROM \(table)").map { result in
var totalResults = 0
var finished : Bool = false
while !finished {
let results = try connection.raw("FETCH \(chunkSize) FROM \(cursorName)").all(decoding: RecRegAction.self).wait()
if results.count > 0 {
totalResults += results.count
print(totalResults)
// Obviously we do our processing here
}
else {
finished = true
}
}
return totalResults
}
}, on: connection)
}.transform(to: ())
}
最佳答案
好的,正如您所知道的,问题出在以下几行:
while ... {
...
try connection.raw("...").all(decoding: RecRegAction.self).wait()
...
}
while
循环和
.wait()
。本质上,这是在事件循环上将异步代码转换为同步代码。这很可能导致死锁,并且肯定会导致其他连接停滞,这就是SwiftNIO尝试检测到该错误并为您提供错误的原因。我不会详细说明为什么它会拖延其他连接,或者为什么这很可能导致此答案陷入僵局。
.wait()
放在不是事件循环线程之一的另一个线程上。为此,任何非EventLoop
线程都可以:DispatchQueue
或您可以使用BlockingIOThreadPool
(不在EventLoop
上运行)Dispatch
和
BlockingIOThreadPool
都具有它们愿意产生的有限数量的线程,因此,如果您经常执行此操作,则可能会耗尽线程,因此将花费更长的时间。
/// delivers partial results (integers) and `nil` if no further elements are available
func deliverPartialResult() -> EventLoopFuture<Int?> {
...
}
func deliverFullResult() -> EventLoopFuture<[Int]>
deliverPartialResult
每次如何返回一个整数,并且
deliverFullResult
传递一个整数数组(即所有整数)。好的,那么我们如何在不调用
deliverFullResult
的情况下编写
deliverPartialResult().wait()
呢?
func accumulateResults(eventLoop: EventLoop,
partialResultsSoFar: [Int],
getPartial: @escaping () -> EventLoopFuture<Int?>) -> EventLoopFuture<[Int]> {
// let's run getPartial once
return getPartial().then { partialResult in
// we got a partial result, let's check what it is
if let partialResult = partialResult {
// another intermediate results, let's accumulate and call getPartial again
return accumulateResults(eventLoop: eventLoop,
partialResultsSoFar: partialResultsSoFar + [partialResult],
getPartial: getPartial)
} else {
// we've got all the partial results, yay, let's fulfill the overall future
return eventLoop.newSucceededFuture(result: partialResultsSoFar)
}
}
}
accumulateResults
,实现
deliverFullResult
不再太困难了:
func deliverFullResult() -> EventLoopFuture<[Int]> {
return accumulateResults(eventLoop: myCurrentEventLoop,
partialResultsSoFar: [],
getPartial: deliverPartialResult)
}
accumulateResults
的作用:
getPartial
,然后在调用它时partialResultsSoFar
一起记住,然后返回(1)nil
意味着partialResultsSoFar
就是我们所能得到的,我们将返回迄今为止已收集的所有内容的新成功的 future accumulateResults(eventLoop: el, partialResultsSoFar: []) {
connection.raw("FETCH \(chunkSize) FROM \(cursorName)")
.all(decoding: RecRegAction.self)
.map { results -> Int? in
if results.count > 0 {
return results.count
} else {
return nil
}
}
}.map { allResults in
return allResults.reduce(0, +)
}
EventLoopFuture<Int>
,其中携带了所有中间
result.count
的总和。
allResults.reduce(0, +)
),这虽然有点浪费,但也并非世界末日。我之所以这样保留它,是因为
accumulateResults
可在您要在数组中累积部分结果的其他情况下使用。
accumulateResults
函数可能在元素类型上是通用的,而且我们可以为外部函数消除
partialResultsSoFar
参数。那这个呢?
func accumulateResults<T>(eventLoop: EventLoop,
getPartial: @escaping () -> EventLoopFuture<T?>) -> EventLoopFuture<[T]> {
// this is an inner function just to hide it from the outside which carries the accumulator
func accumulateResults<T>(eventLoop: EventLoop,
partialResultsSoFar: [T] /* our accumulator */,
getPartial: @escaping () -> EventLoopFuture<T?>) -> EventLoopFuture<[T]> {
// let's run getPartial once
return getPartial().then { partialResult in
// we got a partial result, let's check what it is
if let partialResult = partialResult {
// another intermediate results, let's accumulate and call getPartial again
return accumulateResults(eventLoop: eventLoop,
partialResultsSoFar: partialResultsSoFar + [partialResult],
getPartial: getPartial)
} else {
// we've got all the partial results, yay, let's fulfill the overall future
return eventLoop.newSucceededFuture(result: partialResultsSoFar)
}
}
}
return accumulateResults(eventLoop: eventLoop, partialResultsSoFar: [], getPartial: getPartial)
}
func processPartialResults<T, V>(eventLoop: EventLoop,
process: @escaping (T) -> EventLoopFuture<V>,
getPartial: @escaping () -> EventLoopFuture<T?>) -> EventLoopFuture<V?> {
func processPartialResults<T, V>(eventLoop: EventLoop,
soFar: V?,
process: @escaping (T) -> EventLoopFuture<V>,
getPartial: @escaping () -> EventLoopFuture<T?>) -> EventLoopFuture<V?> {
// let's run getPartial once
return getPartial().then { partialResult in
// we got a partial result, let's check what it is
if let partialResult = partialResult {
// another intermediate results, let's call the process function and move on
return process(partialResult).then { v in
return processPartialResults(eventLoop: eventLoop, soFar: v, process: process, getPartial: getPartial)
}
} else {
// we've got all the partial results, yay, let's fulfill the overall future
return eventLoop.newSucceededFuture(result: soFar)
}
}
}
return processPartialResults(eventLoop: eventLoop, soFar: nil, process: process, getPartial: getPartial)
}
getPartial
,直到返回
nil
为止,但它不会累积所有
getPartial
的结果,而是调用
process
,它获得了部分结果并可以做一些进一步的处理。当
getPartial
EventLoopFuture
返回满足时,将发生下一个
process
调用。
EventLoopFuture
类型,在Vapor中,您将只使用
Future
,但是其余代码应该相同。
关于swift - 使用Vapor 3创建和使用游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51625050/
我正在尝试使用游标遍历表: DEClARE @ProjectOID as nvarchar (100) DECLARE @TaskOID as nvarchar (100) DECLARE TaskO
使用 JOprionPane 时,光标出现了一些问题。我将光标设置到 pharent 框架,然后使用这个显示一个对话框: Object[] possibilities = {"ham", "spam"
我想将数据从一个表(原始数据,所有列都是 VARCHAR)复制到另一个表(使用相应的列格式进行格式化)。 为了将数据从 rawdata 表复制到 formatted 表中,我使用游标来识别受影响的行。
我先走了 我 100% 属于集合运算阵营。但是当设置逻辑时会发生什么在整个所需的输入域上进行检索会导致如此大的检索,以至于查询显着减慢,变得缓慢,或者基本上需要无限的时间? 在这种情况下,我将使用可能
为什么我不能这样做?我想从 TABLEA 中搜索大于光标值的最接近的值,对两者执行平均函数并将结果放入 test3 中。我收到错误代码 1054 未知列“Xnearest in 'field list
我希望以下存储例程返回一系列行,但它只返回 1: CREATE PROCEDURE example() BEGIN DECLARE current_id INT;
我有一张代表患者体检的表,它有检查 ID 和患者 ID。 我想逐行浏览表格并获取每个患者 ID 并比较其不同的咨询,看看它是否被视为“new_attack”。我正在处理疟疾疾病,我们认为每个在过去 6
如文档所述here ,我需要声明一个在打开时接受参数的游标。 我的查询类似于: DECLARE cur CURSOR (argName character varying) FOR SELECT *
我正在尝试使用 PostgreSQL 学习基本游标。这是我的脚本: DECLARE cur_employees CURSOR FOR SELECT * FROM employee CLOS
*DELIMITER // create procedure test(OUT l_out INT) begin DECLARE done INT DEFAULT FALSE; declare l_s
来自 psycopg2 文档: When a database query is executed, the Psycopg cursor usually fetches all the record
我正在使用 while 循环遍历游标,然后输出数据库中每个点的经度和纬度值。 出于某种原因,它没有返回光标中的最后一组(或第一个取决于我是否使用 Cursor.MoveToLast)经度和纬度值。 这
不知道有没有人试过全新的PHPStorm 4 , 但我遇到了这个新版本的问题,而我以前的主要版本 (PHPStorm 3) 没有。 基本上,当我单击代码 View 空白处的任意位置时,光标会设置在该位
mysql的存储过程、游标 、事务实例详解 下面是自己曾经编写过的mysql数据库存储过程,留作存档,以后用到的时候拿来参考。 其中,涉及到了存储过程、游标(双层循环)、事务。 【说明】:代码
Mysql的存储过程是从版本5才开始支持的,所以目前一般使用的都可以用到存储过程。今天分享下自己对于Mysql存储过程的认识与了解。 一些简单的调用以及语法规则这里就不在赘述,网上有许多例子。这里
我正在使用 SQL Server,我有一个包含 3 列(时间序列)的表 data ,带日期,hour开始,AwardStatus . 大部分奖励状态是随机生成的。有两种选择,授予或未授予。 但是,业务
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
Why am getting duplicate records ? pls correct me.Thanks in Advance. declare clazzes_rec clazzes%r
Why am getting duplicate records ? pls correct me.Thanks in Advance. declare clazzes_rec clazzes%r
我需要在数据表中设置一个非唯一标识符。这在组内是连续的,即。对于每个组,ID 应从 1 开始,并以 1 为增量递增,直到该组的最后一行。 下表对此进行了说明。 “新 ID”是我需要填充的列。 Uniq
我是一名优秀的程序员,十分优秀!