Go语言协程池
package main
import (
"fmt"
"time"
driver "github.com/johnnadratowski/golang-neo4j-bolt-driver"
)
var DriverNeo4j driver.Driver
//写入数据库
func CreateNodes(CypherList []string, Connect driver.Conn){
if len(CypherList) > 0{
NilList := make([]map[string]interface{}, len(CypherList))
stmt, _ := Connect.PreparePipeline(CypherList...)
_, errexc := stmt.ExecPipeline(NilList...)
if errexc != nil{
fmt.Println("excute err!!!!!!!!!!!!",errexc)
}
stmt.Close()
}
}
// 一个job的执行任务
func woker(CypherList chan []string){
var cypher_str_arr []string
connect, errcon := DriverNeo4j.OpenNeo("bolt://neo4j:passord@localhost:7687")
if errcon != nil {
fmt.Println("connection err!!!!!!!!!", errcon)
}
for cypher_str_arr = range CypherList{
CreateNodes(cypher_str_arr,connect)
}
connect.Close()
//time.Sleep(1 * time.Second)
}
func main(){
DriverNeo4j = driver.NewDriver()
var cypherList []string
var clearList []string
var cypherstr string
jobch := make(chan []string, 10)
controlch := make(chan int,100)
for i := 0; i< 70000; i++{
cypherstr = fmt.Sprintf(`CREATE (n:JJ{Num:%d})`,i)
cypherList = append(cypherList,cypherstr)
if len(cypherList) % 20 == 0{
templist := cypherList
cypherList = clearList
jobch <- templist
}
}
//workpool
//开始用协程池执行各工作
for w := 0; w < 10; w++{
go woker(jobch)
}
time.Sleep(5 * time.Second)
}