golang调用cerely任务(python起的)

python

celery_app = Celery("app",
                    broker='redis://default:' + REDIS_PASSWORD + '@' + REDIS_HOST + ':' + str(REDIS_PORT) + '/' + '10')
要指定name且唯一
@celery_app.task(time_limit=360, name='task_name')

golang

package main

// Copyright (c) 2019 Sick Yoon
// This file is part of gocelery which is released under MIT license.
// See file LICENSE for full license details.

//package main

import (
	"github.com/gocelery/gocelery"
	"github.com/gomodule/redigo/redis"
	"log"
	"reflect"
	"time"
)

// Run Celery Worker First!
// celery -A worker worker --loglevel=debug --without-heartbeat --without-mingle
func main() {

	// create redis connection pool
	redisPool := &redis.Pool{
		MaxIdle:     3,                 // maximum number of idle connections in the pool
		MaxActive:   0,                 // maximum number of connections allocated by the pool at a given time
		IdleTimeout: 240 * time.Second, // close connections after remaining idle for this duration
		Dial: func() (redis.Conn, error) {
			c, err := redis.DialURL("redis://default:REDIS_PASSWORD@REDIS_HOST:REDIS_PORT/10")
			if err != nil {
				return nil, err
			}
			return c, err
		},
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			_, err := c.Do("PING")
			return err
		},
	}

	// initialize celery client
	cli, _ := gocelery.NewCeleryClient(
		gocelery.NewRedisBroker(redisPool),
		&gocelery.RedisCeleryBackend{Pool: redisPool},
		1,
	)
	log.Println(cli)

	// prepare arguments
	// 定义的任务名称
	taskName := "task_name"
	argA := ""
	argB := "https://sg.xiapibuy.com/WUJU-Plating-Luxury-Wireless-Charging-Transparent-Magnetic-Phone-Case-For-iPhone-14-13-12-11-Pro-Max-Soft-Shockproof-Case-Cover-Magnetic-Clear-i.406057639.19274117521"
	argC := "152"
	argD := "152"
	argE := "test"
	argF := ""
	argG := ""
	argH := ""
	argJ := ""
	// run task
	asyncResult, err := cli.Delay(taskName, argA, argB, argC, argD, argE, argF, argG, argH, argJ)
	if err != nil {
		panic(err)
	}

	// get results from backend with timeout
	res, err := asyncResult.Get(10 * time.Second)
	if err != nil {
		panic(err)
	}

	log.Printf("result: %+v of type %+v", res, reflect.TypeOf(res))

}

你可能感兴趣的:(celery)