Golang проблема параллелизма тикера - PullRequest
0 голосов
/ 17 февраля 2020

У меня есть функция, записанная в Golang следующим образом

func (participant *SimulationParticipant) StartTransactionsGatewayTicker() {
//Gateway
logging.InfoLogger.Printf("StartTransactionsGatewayTicker:%v", participant.Participant)
ticker := time.NewTicker(1 * time.Second)
participant.TransactionGatewayTicker = ticker
go func() {
    for {
        select {
        case <-ticker.C:
            logging.InfoLogger.Printf("Tick at: %v", participant.Participant)
            participant.GetTransactions()
        }
    }
}()
}

Я вызываю функцию в al oop как с 2 SimulationParticipant в массиве. неожиданно первый участник заменяется вторым, и GetTransactions всегда выполняются до последнего элемента в l oop? Как я могу преодолеть это

1 Ответ

1 голос
/ 17 февраля 2020

Работает для меня (я публикую этот код, не видя, как вы вызываете StartTransactionsGatewayTicker, Donot downvote, если не применимо: P):

// [Timers](timers) are for when you want to do
// something once in the future - _tickers_ are for when
// you want to do something repeatedly at regular
// intervals. Here's an example of a ticker that ticks
// periodically until we stop it.

package main

import (
    "fmt"
    "time"
)

func main() {

        part1 := SimulationParticipant{}
    part1.id = "part1"
        part2 := SimulationParticipant{}
        part2.id = "part2"
        partSlice := make([]*SimulationParticipant,0)
        partSlice = append(partSlice, &part1, &part2)

        for _ , p := range partSlice {
             p.StartTransactionsGatewayTicker()
        }

    // Tickers can be stopped like timers. Once a ticker
    // is stopped it won't receive any more values on its
    // channel. We'll stop ours after 16000ms.
    time.Sleep(16000 * time.Millisecond)
    part1.ticker.Stop()
    part2.ticker.Stop()
    fmt.Println("Ticker stopped")
}

type SimulationParticipant struct {
     id string
     ticker *time.Ticker
}

func (participant *SimulationParticipant) StartTransactionsGatewayTicker() {

ticker := time.NewTicker(1 * time.Second)
participant.ticker = ticker
go func() {
    for {
        select {
        case t := <-ticker.C:
            fmt.Println("Tick at", t,participant.id)
        }
    }
}()
}

Вывод:

Tick at 2009-11-10 23:00:01 +0000 UTC m=+1.000000001 part2
Tick at 2009-11-10 23:00:01 +0000 UTC m=+1.000000001 part1
Tick at 2009-11-10 23:00:02 +0000 UTC m=+2.000000001 part1
Tick at 2009-11-10 23:00:02 +0000 UTC m=+2.000000001 part2
Tick at 2009-11-10 23:00:03 +0000 UTC m=+3.000000001 part2
Tick at 2009-11-10 23:00:03 +0000 UTC m=+3.000000001 part1
Tick at 2009-11-10 23:00:04 +0000 UTC m=+4.000000001 part1
Tick at 2009-11-10 23:00:04 +0000 UTC m=+4.000000001 part2
Tick at 2009-11-10 23:00:05 +0000 UTC m=+5.000000001 part2
Tick at 2009-11-10 23:00:05 +0000 UTC m=+5.000000001 part1
Tick at 2009-11-10 23:00:06 +0000 UTC m=+6.000000001 part1
Tick at 2009-11-10 23:00:06 +0000 UTC m=+6.000000001 part2
Tick at 2009-11-10 23:00:07 +0000 UTC m=+7.000000001 part2
Tick at 2009-11-10 23:00:07 +0000 UTC m=+7.000000001 part1
Tick at 2009-11-10 23:00:08 +0000 UTC m=+8.000000001 part1
Tick at 2009-11-10 23:00:08 +0000 UTC m=+8.000000001 part2
Tick at 2009-11-10 23:00:09 +0000 UTC m=+9.000000001 part2
Tick at 2009-11-10 23:00:09 +0000 UTC m=+9.000000001 part1
Tick at 2009-11-10 23:00:10 +0000 UTC m=+10.000000001 part1
Tick at 2009-11-10 23:00:10 +0000 UTC m=+10.000000001 part2
Tick at 2009-11-10 23:00:11 +0000 UTC m=+11.000000001 part2
Tick at 2009-11-10 23:00:11 +0000 UTC m=+11.000000001 part1
Tick at 2009-11-10 23:00:12 +0000 UTC m=+12.000000001 part1
Tick at 2009-11-10 23:00:12 +0000 UTC m=+12.000000001 part2
Tick at 2009-11-10 23:00:13 +0000 UTC m=+13.000000001 part2
Tick at 2009-11-10 23:00:13 +0000 UTC m=+13.000000001 part1
Tick at 2009-11-10 23:00:14 +0000 UTC m=+14.000000001 part1
Tick at 2009-11-10 23:00:14 +0000 UTC m=+14.000000001 part2
Tick at 2009-11-10 23:00:15 +0000 UTC m=+15.000000001 part2
Tick at 2009-11-10 23:00:15 +0000 UTC m=+15.000000001 part1
Ticker stopped

Детская площадка : https://play.golang.org/p/yfHnrRK1iG8

...