goroutine

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 26, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Go = func(f func()) {
	globalG.Go(f)
}

Go 暴露到外界到可控写成方法

Functions

This section is empty.

Types

type GoRoutine

type GoRoutine struct {
	// contains filtered or unexported fields
}

GoRoutine 协程池

func New

func New(limit, multiple int) *GoRoutine

New 创建一个协程池

limit: 协程池限制
multiple: 缓冲区大小
Example
package main

import (
	"context"
	"fmt"
	"time"

	goroutine "github.com/aide-family/moon/pkg/util/go"
)

func main() {
	limit := 2
	multiple := 2
	g := goroutine.New(limit, multiple)
	ctx := context.Background()
	if err := g.Start(ctx); err != nil {
		panic(err)
	}

	for i := range 20 {
		item := i
		goroutine.Go(func() {
			time.Sleep(100 * time.Millisecond)
			fmt.Println("msg: ", item)
		})
	}

	time.Sleep(time.Second * 4)

	if err := g.Stop(ctx); err != nil {
		panic(err)
	}
}
Output:

func (*GoRoutine) Go

func (g *GoRoutine) Go(f func())

Go 协程池执行任务

func (*GoRoutine) Start

func (g *GoRoutine) Start(_ context.Context) error

Start 启动协程池

func (*GoRoutine) Stop

func (g *GoRoutine) Stop(_ context.Context) error

Stop 停止协程池

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL