Business logic .
Start a fixed number of coroutines and listen to chan. Then turn the query data into CSV Simulation program.
db,err := ConnectDatabase(ctx)
if err != nil {
return err
}
// for i := 0; i < 10000; i++ {
// s := fmt.Sprintf("create table test_more.table_%04v(id bigint,col1 varchar(10))",i)
// db.Exec(s)
// s = fmt.Sprintf("insert into test_more.table_%04v values (1,'col1')",i)
// db.Exec(s)
// }
doSelect := func(i int64) {
s := fmt.Sprintf("select id, col1 from test_more.table_%04v ",i)
rows, err := db.Query(s)
if err != nil {
if strings.Contains(err.Error(),"undefined name") {
return
}
panic(err)
}
// defer rows.Close()
for rows.Next() {
var (
v string
id int
)
if err := rows.Scan(&id, &v); err != nil {
panic(err)
}
}
rows.Close()
// fmt.Println(i)
}
taskChan := make(chan int64, 20)
doTableDataTaskWorker := func() {
for {
select {
case i, ok := <-taskChan:
if !ok {
return
}
doSelect(i)
}
}
}
wg := sync.WaitGroup{}
parallel := 4
wg.Add(parallel)
for i := 0; i < parallel; i++ {
go func(workNum int) {
defer wg.Done()
doTableDataTaskWorker()
}(i)
}
go func() {
for i := 0; i < 10000; i++ {
taskChan <- int64(i)
}
close(taskChan)
}()
wg.Wait()
db.Close()
Business logic .
Start a fixed number of coroutines and listen to chan. Then turn the query data into CSV Simulation program.
go envOutput: