首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Golang并发R/W到数据库

Golang并发R/W到数据库
EN

Stack Overflow用户
提问于 2020-06-02 17:59:13
回答 1查看 1.6K关注 0票数 2

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

在完成所有写入之后,系统会执行一些额外的读取操作,这将导致查询返回不正确结果的问题,因为并非所有数据都已写入表中。因为我正在提取的JSON数据是动态的,所以我无法很容易地知道何时编写了所有数据。

我考虑过解决这一问题的两种可能性,尽管我对这两种解决方案都不太满意:

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

为了纠正这个问题,我还需要考虑其他的设计决策吗?作为参考,我用来提取这些数据的库是go-colly和go-sqlite3 3。感谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-02 18:56:29

您可以使用sync.WaitGroup

例如:

代码语言:javascript
复制
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.
    // ...
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62158372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档