第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

gocron 使用參數(shù)動態(tài)添加和刪除任務(wù)

gocron 使用參數(shù)動態(tài)添加和刪除任務(wù)

Go
長風(fēng)秋雁 2022-08-01 10:14:38
我正在使用我當(dāng)前的項(xiàng)目,我遇到了一些文檔中沒有的情況。gocron我測試這個代碼:gocron.Every(3).Seconds().Do(taskWithParams,2,"world")gocron.Every(2).Seconds().Do(taskWithParams,1, "hello")gocron.Start()time.Sleep(10 * time.Second)gocron.Remove(taskWithParams)//<-- remove task...func taskWithParams(a int, b string) {    fmt.Println(a, b)}當(dāng)我刪除 task() 時,總是被刪除。即使我交換它們:gocron.Remove(taskWithParams)gocron.Every(3).Seconds().Do(taskWithParams,2,"world")gocron.Every(2).Seconds().Do(taskWithParams,1, "hello")gocron.Every(3).Seconds().Do(taskWithParams,2,"world")有沒有辦法讓我具體指出我要刪除的任務(wù),因?yàn)槲ㄒ辉试S1?remove()argument該文檔還有一個:schedulers := gocron.NewScheduler()    s.Every(3).Seconds().Do(task)    <- s.Start()什么時候是最好的用例?scheduler如果我們完成了調(diào)度程序,如何將其從內(nèi)存中刪除?做工作嗎?或者我們必須有另一種方法將它們從記憶中清除?scheduler.Clear()
查看完整描述

1 回答

?
函數(shù)式編程

TA貢獻(xiàn)1807條經(jīng)驗(yàn) 獲得超9個贊

您可以通過對函數(shù)處理程序進(jìn)行重復(fù)數(shù)據(jù)刪除來處理刪除邏輯。


package main


import (

    "fmt"

)


func main() {

    fn1 := func() { taskWithParams(2, "world") }

    gocron.Every(3).Seconds().Do(fn1)

    fn2 := func() { taskWithParams(1, "hello") }

    gocron.Every(2).Seconds().Do(fn2)

    gocron.Start()


    time.Sleep(10 * time.Second)

    gocron.Remove(fn2)

}

func taskWithParams(a int, b string) {

    fmt.Println(a, b)

}

否則,該方法將返回一個可以傳遞給 的實(shí)例。scheduler.Do*Jobscheduler.RemoveByReference


package main


import (

    "fmt"

)


func main() {


    job, err := gocron.Every(3).Seconds().Do(taskWithParams, 2, "ww")

    if err != nil {

        panic(err)

    }

    gocron.Every(2).Seconds().Do(taskWithParams, 1, "hh")

    gocron.Start()


    time.Sleep(10 * time.Second)

    gocron.RemoveByReference(job)

}

func taskWithParams(a int, b string) {

    fmt.Println(a, b)

}


查看完整回答
反對 回復(fù) 2022-08-01
  • 1 回答
  • 0 關(guān)注
  • 397 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號