简体   繁体   English

Grails CSV 插件 - 并发

[英]Grails CSV Plugin - Concurrency

I am using the plugin: Grails CSV Plugin in my application with Grails 2.5.3 .我正在使用插件: Grails CSV Plugin in my application with Grails 2.5.3 I need to implement the concurrency functionality with for example: GPars , but I don't know how I can do it.我需要实现并发功能,例如: GPars ,但我不知道我该怎么做。

Now, the configuration is sequential processing.现在,配置是顺序处理。 Example of my code fragment:我的代码片段示例:

Thanks.谢谢。

Implementing concurrency in this case may not give you much of a benefit.在这种情况下实现并发可能不会给您带来太多好处。 It really depends on where the bottleneck is.这实际上取决于瓶颈在哪里。 For example, if the bottleneck is in reading the CSV file, then there would be little advantage because the file can only be read in sequential order.例如,如果瓶颈在于读取 CSV 文件,那么就没有什么优势,因为文件只能按顺序读取。 With that out of the way, here's the simplest example I could come up with:顺便说一下,这是我能想到的最简单的例子:

import groovyx.gpars.GParsPool

def tokens = csvFileLoad.inputStream.toCsvReader(['separatorChar': ';', 'charset': 'UTF-8', 'skipLines': 1]).readAll()
def failedSaves = GParsPool.withPool {
    tokens.parallel
        .map { it[0].trim() }
        .filter { !Department.findByName(it) }
        .map { new Department(name: it) }
        .map { customImportService.saveRecordCSVDepartment(it) }
        .map { it ? 0 : 1 }
        .sum() 
}

if(failedSaves > 0) transactionStatus.setRollbackOnly()

As you can see, the entire file is read first;如您所见,首先读取整个文件; hence the main bottleneck.因此是主要瓶颈。 The majority of the processing is done concurrently with the map() , filter() , and sum() methods.大多数处理与map()filter()sum()方法同时完成。 At the very end, the transaction is rolled back if any of the Department s failed to save.最后,如果Department的任何一个未能保存,则事务将回滚。

Note: I chose to go with a map() - sum() pair instead of using anyParallel() to avoid having to convert the parallel array produced by map() to a regular Groovy collection, perform the anyParallel() , which creates a parallel array and then converts it back to a Groovy collection.注意:我选择使用map() - sum()对而不是使用anyParallel()以避免必须将map()生成的并行数组转换为常规 Groovy 集合,执行anyParallel() ,这会创建一个并行数组,然后将其转换回 Groovy 集合。

Improvements改进

As I already mentioned in my example the CSV file is first read completely before the concurrent execution begins.正如我在我的示例中已经提到的,在并发执行开始之前首先完全读取 CSV 文件。 It also attempts to save all of the Department instances, even if one failed to save.它还尝试保存所有Department实例,即使其中一个未能保存。 You may want that (which is what you demonstrated) or not.您可能想要(这就是您所展示的)或不想要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM