基于golang channel的轻量级异步任务分发器

项目地址:https://github.com/chenhg5/go…

有时候我们为了更好的利用计算机资源,可以把一些耗时长的任务队列化异步执行。举个对应简单的生活中例子就是大多数餐厅里面点菜都是先找地方做,看了菜单选好菜之后找服务员点菜,此时再等待菜做好送上来。这里餐厅厨房就是计算机的底层资源,菜就是待执行的任务,而服务员就是我们的go channel。

关于消息队列有很多好用的框架,如nsq,nats,kafka等等。但有时我们只需要轻量级的异步任务工具,而不需要太过于”复杂”的框架相对于我们的需求来说。于是借鉴一些项目框架,做了一个小小的封装。

go-task的使用很简单,只需要初始化一个任务处理器,然后往处理器里面添加任务,然后处理器就会异步地去执行了。

举餐厅的例子,代码如下:

package main

import (
    "runtime"
    "fmt"
    "time"
    "github.com/chenhg5/go-task"
    "strconv"
)

func main() {

    // init
    task.InitTaskReceiver(runtime.NumCPU())

    // 有十个人同时点菜
    for i := 0; i < 10; i++ {
        task.AddTask(task.NewTask(
            map[string]interface{}{
                "user" : strconv.Itoa(i),
            },  // 参数
            []task.FacFunc{ordering, cooking, deliverying}, // 任务列表
            -1), // -1代表任务不超时
        )
    }

    time.Sleep(time.Second * 50)
}

// 下单任务
func ordering(uuid string, param map[string]interface{}) (string, error) {
    fmt.Println("user " + param["user"].(string) + " is ordering")
    time.Sleep(time.Second * 1)
    return uuid, nil
}

// 做菜任务
func cooking(uuid string, param map[string]interface{}) (string, error) {
    fmt.Println("user " + param["user"].(string) + " is cooking")
    time.Sleep(time.Second * 1)
    return uuid, nil
}

// 配送任务
func deliverying(uuid string, param map[string]interface{}) (string, error) {
    fmt.Println("user " + param["user"].(string) + " is deliverying")
    time.Sleep(time.Second * 1)
    return uuid, nil
}

跑一遍,就会看到:

user 0 is ordering
user 1 is ordering
user 2 is ordering
user 3 is ordering
user 4 is ordering
user 5 is ordering
user 6 is ordering
user 7 is ordering
user 0 is cooking
user 2 is cooking
user 1 is cooking
user 3 is cooking
user 4 is cooking
user 5 is cooking
user 6 is cooking
user 7 is cooking
user 0 is deliverying
user 1 is deliverying
user 2 is deliverying
user 3 is deliverying
user 6 is deliverying
user 7 is deliverying
user 5 is deliverying
user 4 is deliverying
user 8 is ordering
user 9 is ordering
user 8 is cooking
user 9 is cooking
user 9 is deliverying
user 8 is deliverying
    原文作者:honhon
    原文地址: https://segmentfault.com/a/1190000015449610
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞