golang 获取当前线程的id
gospider 介绍
gospider 是一个golang 爬虫神器,拥有python到golang爬虫过渡的所有必需库。用于python爬虫从业者快速且无坑的过渡到golang
安装
go get -u gitee.com/baixudong/gospider
gitee地址
https://gitee.com/baixudong/gospider
github地址
https://github.com/baixudong007/gospider
代码示例
package main
import (
"context"
"log"
"time"
"gitee.com/baixudong/gospider/thread"
)
func test(ctx context.Context, num int) {
log.Printf("第%d个线程池中的第%d个请求开始", thread.GetThreadId(ctx), num)
time.Sleep(time.Second)
log.Printf("第%d个线程池中的第%d个请求结束", thread.GetThreadId(ctx), num)
}
func main() {
threadCli := thread.NewClient(nil, 3) //限制并发为3
for i := 0; i < 10; i++ {
//读取任务
threadCli.Write(&thread.Task{
Func: test,
Args: []any{i},
})
}
threadCli.Join()
}