mirror of
https://github.com/hicccc77/WeFlow.git
synced 2026-03-25 07:16:51 +00:00
Refactor concurrency management in decryptOne function
Refactor concurrency handling in decryption process to use a Set for tracking promises instead of an array. This improves performance by simplifying the removal of completed promises.
This commit is contained in:
@@ -3485,20 +3485,17 @@ function ChatPage(props: ChatPageProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 并发池:同时跑 concurrency 个任务
|
// 并发池:同时跑 concurrency 个任务
|
||||||
const pool: Promise<void>[] = []
|
const pool = new Set<Promise<void>>()
|
||||||
for (const img of images) {
|
for (const img of images) {
|
||||||
const p = decryptOne(img)
|
const p = decryptOne(img).then(() => { pool.delete(p) })
|
||||||
pool.push(p)
|
pool.add(p)
|
||||||
if (pool.length >= concurrency) {
|
if (pool.size >= concurrency) {
|
||||||
await Promise.race(pool)
|
await Promise.race(pool)
|
||||||
// 移除已完成的
|
|
||||||
for (let j = pool.length - 1; j >= 0; j--) {
|
|
||||||
const settled = await Promise.race([pool[j].then(() => true), Promise.resolve(false)])
|
|
||||||
if (settled) pool.splice(j, 1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (pool.size > 0) {
|
||||||
await Promise.all(pool)
|
await Promise.all(pool)
|
||||||
|
}
|
||||||
|
|
||||||
finishDecrypt(successCount, failCount)
|
finishDecrypt(successCount, failCount)
|
||||||
}, [batchImageMessages, batchImageSelectedDates, batchDecryptConcurrency, currentSessionId, finishDecrypt, sessions, startDecrypt, updateDecryptProgress])
|
}, [batchImageMessages, batchImageSelectedDates, batchDecryptConcurrency, currentSessionId, finishDecrypt, sessions, startDecrypt, updateDecryptProgress])
|
||||||
|
|||||||
Reference in New Issue
Block a user