github.com/rolandhe/saber@v0.0.4/example/executor/main.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/rolandhe/saber/gocc"
     5  	"log"
     6  	"time"
     7  )
     8  
     9  func main() {
    10  	//execGroupTask()
    11  	execSingleTask()
    12  }
    13  
    14  func execSingleTask() {
    15  	executor := gocc.NewDefaultExecutor(10)
    16  	f, ok := executor.ExecuteTimeout(func() (any, error) {
    17  		time.Sleep(time.Millisecond * 100)
    18  		return 325, nil
    19  	}, time.Millisecond*3)
    20  	if !ok {
    21  		log.Println("error")
    22  		return
    23  	}
    24  	ret, err := f.Get()
    25  	if err != nil {
    26  		log.Println(err)
    27  		return
    28  	}
    29  	v := ret.(int)
    30  	log.Println(v)
    31  }
    32  
    33  func execGroupTask() {
    34  	executor := gocc.NewDefaultExecutor(10)
    35  	fg := gocc.NewFutureGroup(100)
    36  	for i := 0; i < 100; i++ {
    37  		_, ok := executor.ExecuteInGroupTimeout(func() (any, error) {
    38  			time.Sleep(time.Millisecond * 100)
    39  			return 325, nil
    40  		}, fg, time.Second*1)
    41  		if !ok {
    42  			log.Println("error")
    43  			return
    44  		}
    45  	}
    46  
    47  	futures := fg.Get()
    48  	for _, f := range futures {
    49  		v, _ := f.Get()
    50  		log.Println(v)
    51  	}
    52  }