一个 Golang 工作池,其中工作人员自己打开和关闭 SQL 连接。

huangapple go评论77阅读模式
英文:

A Golang Worker Pool, where the workers themselves open and close the SQL Connections

问题

我正在尝试使用Go的工作池来加速我的并发工作脚本,但到目前为止没有成功。我花了很多小时才写下这篇帖子...

首先,我使用了这个库:https://github.com/gammazero/workerpool,在一开始效果非常好。但问题是,当我尝试同时从所有工作中写入同一个SQL表时,随机时间后就会出现一些段错误。然后有人建议我为每个工作线程打开一个新的SQL连接。但是这个库本身设计上不允许我控制工作线程。

然后我尝试了这里的代码:https://gobyexample.com/worker-pools,并在worker()的for-range循环的顶部和底部加入了SQL的打开和关闭操作。但是我没有成功。尽管我向池中推送了数千个任务,只有16个任务被执行(每个工作线程执行一个任务,总共有16个工作线程),然后就停止了。我不知道为什么会这样。

你们中的任何人能否为我提供一个关于Golang工作池的实例代码,其中每个工作线程都控制着SQL连接?

顺便说一下,我使用的是https://gorm.io作为MySQL的ORM,到目前为止效果非常好。

也许你们中的任何人都可以帮助我。我也可以提供更多细节。
这将极大地加快我的导入脚本速度。

再见
Adrian

英文:

i was experimenting with Go worker pools to speed up my import script with concurrent workers. But without any luck by now. And i spend lot of hours until i wrote this post...

First i used this one https://github.com/gammazero/workerpool, which worked out very well in first place. But the problem here, is that i submit Jobs to some 'pool' and these jobs are executed. When i try write to the same SQL Table at the same time from all jobs, i get some Segmentation fault after random amount of time. Then i got the advice to open a new sql connection per worker. And that is by design not possible with the upper library, as i have no control about the workers themselves.

Then i tried the code from here https://gobyexample.com/worker-pools and entered the SQL Open and Close at the top and the bottom of the worker() for-range. But i had no luck. Although i pushed 1000s of jobs to the Pool, only 16 jobs where taken (1 by each worker, with 16 workers) and stop. And i have no clue why.

Could anyone of you provide me a solid example code about a golang worker pool, where each worker controls the SQL Connection ?

By the way, i use https://gorm.io as MySQL ORM, which works out very well up to this point.

Maybe anyone of you could help me. I can also provide you with further details.
That would speed up my import script massively.

Bye then
Adrian

答案1

得分: 0

我终于发现了,打开和关闭MySQL连接并没有导致段错误。相反,是一些产品中的数据引起了问题。在添加了一个延迟函数来从段错误中恢复之后,一切都运行得很顺利。

英文:

i finally found out, that the opening and closing of the mysql connection didnt cause the segfault. it was rather the data inside some of the products. after adding a defer func to recover from segfaults everyting is running smoothly.

huangapple
  • 本文由 发表于 2023年2月1日 02:27:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75301596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定