Giter VIP home page Giter VIP logo

async's Introduction

golang async

简介

通过golang的goruntine来提供一种异步处理程序的能力。

应用场景

这在多个耗时长的网络请求(如:调用API接口)时非常有用。其可将顺序执行变为并行计算,可极大提高程序的执行效率。也能更好的发挥出多核CPU的优势。

使用

go get github.com/freshcn/async

demo

// 建议程序开启多核支持
runtime.GOMAXPROCS(runtime.NumCPU())

// 耗时操作1
func request1()interface{}{
  //sql request...
}

// 耗时操作2
func request2()interface{}{
  //sql request...
}

// 新建一个async对象
async := async.New()

// 添加request1异步请求,第一个参数为该异步请求的唯一logo,第二个参数为异步完成后的回调函数,回调参数类型为func()interface{}
async.Add("request1",request1)
// 添加request2异步请求
async.Add("request2",request2)

// 执行
if chans,ok := async.Run();ok{
    // 将数据从通道中取回,取回的值是一个map[string]interface{}类型,key为async.Add()时添加的logo,interface{}为该logo回调函数返回的结果
    res := <-chans
    // 这里最好判断下是否所有的异步请求都已经执行成功
		if len(res) == 2 {
			for k, v := range res {
				//do something
			}
		} else {
			log.Println("async not execution all task")
		}
}

// 清除掉本次操作的所以数据,方便后续继续使用async对象
async.Clean()

async's People

Contributors

freshcn avatar scofieldpeng avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.