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

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

我如何等待通道活動的間歇來觸發(fā)某些事情?

我如何等待通道活動的間歇來觸發(fā)某些事情?

Go
不負相思意 2021-12-20 19:07:05
我有一個通道可以接收突發(fā)寫入。我想等到通道上的突發(fā)發(fā)送完成后再觸發(fā)操作。我已經(jīng)看過這個gist,但是,interval如果緩沖區(qū)中有數(shù)據(jù),它就會發(fā)送輸出:func debounceChannel(interval time.Duration, output chan int) chan int {  input := make(chan int)  go func() {    var buffer int    var ok bool    // We do not start waiting for interval until called at least once    buffer, ok = <-input     // If channel closed exit, we could also close output    if !ok {      return    }    // We start waiting for an interval    for {      select {      case buffer, ok = <-input:        // If channel closed exit, we could also close output        if !ok {          return        }      case <-time.After(interval):        // Interval has passed and we have data, so send it        output <- buffer        // Wait for data again before starting waiting for an interval        buffer, ok = <-input        if !ok {          return        }        // If channel is not closed we have more data and start waiting for interval      }    }  }()  return input}在我的情況下,我想等到在觸發(fā)或發(fā)送輸出之前不再在輸入通道上為此突發(fā)發(fā)送任何數(shù)據(jù)。我如何實現(xiàn)這一目標?
查看完整描述

2 回答

?
炎炎設(shè)計

TA貢獻1808條經(jīng)驗 獲得超4個贊

聽起來你需要在 goroutines 之間進行同步,也許沿著這條線。


func main() {


        // Create a channel for our input

        input := make(chan int, 1)

        // Create another for synchronization between main and forked goroutines

        done := make(chan bool)


        go func() {

                // block-wait for received value

                <-input


                // do some more things here


                // when done, send signal to the main goroutine

                done <- true

        }()


        // Do something while wait for the forked goroutine


        // this block until `<-done`

        <-done

        close(mychan)

}

這篇文章非常清楚地解釋了使用通道和同步組進行同步。


查看完整回答
反對 回復(fù) 2021-12-20
?
慕工程0101907

TA貢獻1887條經(jīng)驗 獲得超5個贊

這就是我最終作為我的 debouncer 實現(xiàn)的:


func Debounce(lull time.Duration, in chan struct{}, out chan struct{}) {


    go func() {


        var last int64 = 0


        for {

            select {

            case <-in:

                last = time.Now().Unix()


            case <-time.Tick(lull):

                if last != 0 && time.Now().Unix() >= last+int64(lull.Seconds()) {

                    last = 0

                    out <- struct{}{}

                }

            }

        }

    }()

}

它需要一個間歇時間,即如果我們沒有收到輸入的持續(xù)時間,那么我們假設(shè)數(shù)據(jù)突發(fā)中存在中斷。有2個通道,1個輸入和1個輸出。數(shù)據(jù)突發(fā)到達輸入端,對于每個突發(fā),我們在突發(fā)結(jié)束時寫入輸出通道。


實現(xiàn)非常簡單。每次從輸入通道接收時,我只存儲當前的 unix 時間戳。然后,我有一個停頓時間的自動收報機。所有這些都是檢查我們是否已經(jīng)超過了最后一次突發(fā)的等待時間。如果是這樣,它會重置last為 0,并在輸出通道上發(fā)出一個事件。


下面是一些使用 debounce 函數(shù)的代碼,它的靜止時間為 2 秒,它在輸入通道上發(fā)送隨機突發(fā):


func main() {


    out := make(chan struct{})

    in := make(chan struct{})


    Debounce(2*time.Second, in, out)


    // Generating bursts of input data

    go func(in chan struct{}) {


        for {

            select {

            case <-time.Tick(1 * time.Second):

                in <- struct{}{}


                fmt.Println("Sending!")


                shouldSleep := rand.Intn(2)

                if shouldSleep == 1 {

                    time.Sleep(5 * time.Second)

                }

            }

        }

    }(in)


    // Listening for output events

    go func(out chan struct{}) {


        for _ = range out {

            fmt.Println("Got an event!")

        }

    }(out)


    // Do not let main terminate.

    done := make(chan struct{})

    <-done

}


查看完整回答
反對 回復(fù) 2021-12-20
  • 2 回答
  • 0 關(guān)注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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