Code Monkey home page Code Monkey logo

asynctask's Introduction

Async Task

安装

go get github.com/wanghaha-dev/asyncTask

简单使用

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/wanghaha-dev/asyncTask"
)

func main() {
	task, err := asyncTask.NewTask(context.Background(), asyncTask.Config{
		Addr:     "127.0.0.1:6379",
		DB:       0,
		Password: "",
	})
	if err != nil {
		panic(err)
	}

	// put task
	taskId, err := asyncTask.GenerateTaskId()
	if err != nil {
		log.Fatalln("generate taskId error!")
	}
	task.PutNormalTask(taskId, asyncTask.Map{
		"name": "task1",
		"date": asyncTask.Datetime(),
		"user": "1001",
	})

	// take task
	t1, err := task.TakeNormalTask()
	if err != nil {
		log.Fatalln("take normal task error:", err)
	}

	fmt.Println("take task is: ", t1)
	fmt.Println(t1["task_id"], t1["data"])
	fmt.Println(t1["data"].(map[string]interface{})["name"])
	jsonContext, err := t1.GetJSON()
	if err != nil {
		log.Fatalln("GetJson error: ", err)
	}
	fmt.Println(t1.GetJSON())
	// {"data":{"date":"2022-08-26 10:26:44","name":"task1","user":"1001"},"task_id":"03efb9e08b284642b5fd1ea11ae20341"}
	fmt.Println(asyncTask.JSONGet(jsonContext, "data.name"))
}

并发使用

package main

import (
	"context"
	"fmt"
	"log"
	"sync"

	"github.com/wanghaha-dev/asyncTask"
)

func main() {
	task, err := asyncTask.NewTask(context.Background(), asyncTask.Config{
		Addr:     "127.0.0.1:6379",
		DB:       0,
		Password: "",
	})
	if err != nil {
		panic(err)
	}

	var wg sync.WaitGroup
	wg.Add(200)

	// put task
	go func() {
		asyncTask.Each(100, func() {
			taskId, err := asyncTask.GenerateTaskId()
			if err != nil {
				log.Fatalln("generate taskId error!")
			}
			task.PutNormalTask(taskId, asyncTask.Map{
				"name": "task1",
				"date": asyncTask.Datetime(),
				"user": "1001",
			})

			wg.Done()
		})
	}()

	// take task
	go func() {
		asyncTask.Each(100, func() {
			t1, err := task.TakeNormalTask()
			if err != nil {
				log.Fatalln("take normal task error:", err)
			}

			fmt.Println("take task is: ", t1)

			wg.Done()
		})
	}()

	wg.Wait()
	fmt.Println("finish.")
}

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.