Golang 并发 R/W 到数据库

Golang concurrent R/W to database

我正在编写一些 Go 软件,负责下载和解析大量 JSON 文件并将解析后的数据写入 sqlite 数据库。我当前的设计同时具有 10 个 go 例程 downloading/parsing 这些 JSON 并将它们与另一个 go 例程通信,该例程的唯一工作是侦听特定通道并将通道内容写入数据库。

系统在本应完成所有写入后执行一些额外的读取操作,这导致查询 return 错误结果的问题,因为并非所有数据都已写入 table .因为我拉取的 JSON 数据是动态的,所以我无法知道所有数据何时写入。

我考虑过两种解决方案,但我对这两种解决方案都不太满意:

  1. 在频道上收听并等待它为空。这原则上应该可行,但是,它不能确保数据已写入,它只能确保已在通道上接收到数据。
  2. 同步对数据库的访问。这在原则上应该再次起作用,但是,我仍然需要在所有写入操作之后命令查询操作。

我是否应该考虑任何其他设计决策来纠正此问题?作为参考,我用来提取这些数据的库是 go-colly 和 go-sqlite3。感谢所有帮助!

您可以使用 sync.WaitGroup

例如

package main

import "sync"

func main() {
    // Some sort of job queue for your workers to process. This job queue should be closed by the process
    // that populates it with items. Once the job channel is closed, any for loops ranging over the channel
    // will read items until there are no more items, and then break.
    jobChan := make(chan JobInfo)

    // Populate the job queue here...
    // ...
    close(jobChan)

    // We now have a full queue of jobs that can't accept new jobs because the channel is closed.

    // Number of concurrent workers.
    workerCount := 10

    // Initialize the WaitGroup.
    wg := sync.WaitGroup{}
    wg.Add(workerCount)

    // Create the worker goroutines.
    for i := 0; i < workerCount; i++ {
        go func() {
            // When the jobChan is closed, and no more jobs are available on the queue, the for loop
            // will exit, causing wg.Done() to be called, and the anonymous function to exit.
            for job := range jobChan {
                // Process job.
            }
            wg.Done()
        }()
    }

    // Wait for all workers to call wg.Done()
    wg.Wait()

    // Whatever you want to do after all queue items have been processed goes here.
    // ...
}