所以我有一個程式,我從資料庫中獲取檔案路徑串列,洗掉檔案系統上的這些檔案,最后從資料庫中洗掉檔案路徑。我將所有操作放在一個事務中,以確保當檔案系統中的所有檔案都被洗掉時,路徑將從資料庫中洗掉。
像這樣的東西
val result = for {
deletePath <- (fr""" select path from files""").query[String].stream //Stream[doobie.ConnectionIO,String]
_ <- Stream.eval(AsyncConnectionIO.liftIO(File(deletePath).delete()) //Stream[doobie.ConnectionIO,Unit]
_ <- Stream.eval(sql"delete from files where path = ${deletePath}".withUniqueGeneratedKeys)
}
result.compile.drain.transact(transactor)
不幸的是,檔案系統是分布式的,這意味著單個操作很慢,但它允許一次進行多個操作。
所以我的問題是,我如何在這里并行化檔案系統洗掉操作?
uj5u.com熱心網友回復:
是的,你可以。只需使用適當的組合符而不是for語法。
val result =
(fr""" select path from files""")
.query[String]
.stream
.parEvalMapUnordered(maxConcurrent = 64) { deletePath =>
AsyncConnectionIO.liftIO(File(deletePath).delete()) >>
sql"delete from files where path = ${deletePath}".withUniqueGeneratedKeys
}
result.compile.drain.transact(transactor)
請記住將maxConcurrent引數更改為對您的用例有意義的引數。
(我無法測驗代碼,所以它可能有一些拼寫錯誤)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/459750.html
