我正在编写一些Go软件,该软件负责下载和解析大量JSON文件,并将解析过的数据写入sqlite数据库。我目前的设计有10个go例程,同时下载/解析这些JSON并将它们通信到另一个go例程,后者的唯一工作是在特定频道上侦听并将通道内容写入DB。
在完成所有写入之后,系统会执行一些额外的读取操作,这将导致查询返回不正确结果的问题,因为并非所有数据都已写入表中。因为我正在提取的JSON数据是动态的,所以我无法很容易地知道何时编写了所有数据。
我考虑过解决这一问题的两种可能性,尽管我对这两种解决方案都不太满意:
为了纠正这个问题,我还需要考虑其他的设计决策吗?作为参考,我用来提取这些数据的库是go-colly和go-sqlite3 3。感谢你的帮助!
发布于 2020-06-02 18:56:29
您可以使用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.
// ...
}https://stackoverflow.com/questions/62158372
复制相似问题