Giter Club home page Giter Club logo

Comments (6)

agoalofalife avatar agoalofalife commented on August 30, 2024 1

@rdelval can write an example of how you use faker in multiple goroutines?

from faker.

agoalofalife avatar agoalofalife commented on August 30, 2024

on this topic I've found this discussion

from faker.

ridv avatar ridv commented on August 30, 2024

Sure, the following should trigger

panic: runtime error: index out of range

goroutine 6 [running]:
math/rand.(*rngSource).Uint64(...)
        /Users/<username>/.gvm/gos/go1.9.4/src/math/rand/rng.go:246
math/rand.(*rngSource).Int63(0xc420083500, 0x4519710ecadc72e8)
        /Users/<username>.gvm/gos/go1.9.4/src/math/rand/rng.go:231 +0x8a
github.com/bxcodec/faker.randomString(0x19, 0x18, 0xc420083500)
        /Users/r<username>/go/src/github.com/bxcodec/faker/faker.go:299 +0x10c
github.com/bxcodec/faker.getValue(0x1178c40, 0x10c7780, 0x10c7780, 0xc420010360, 0x16, 0x98, 0x0)
        /Users/<username>/go/src/github.com/bxcodec/faker/faker.go:182 +0x1710
github.com/bxcodec/faker.FakeData(0x10c3f40, 0xc420010360, 0x0, 0x0)
        /Users/ <username>/go/src/github.com/bxcodec/faker/faker.go:131 +0x1d6
main.main.func1(0xc42001a2a0, 0xc420010360)
        /Users/<username>/testarea/test.go:21 +0x6a
created by main.main
        /Users/<username>/testarea/test.go:17 +0x9c
exit status 2
package main

import (
	"sync"

	"github.com/bxcodec/faker"
)

func main() {
	var wg sync.WaitGroup
	for i := 0; i < 40; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()

                         // Need a fixed number of strings in an array
			var name string 
			lst := make([]string, 0, 1000)

			for x := 0; x < 1000; x++ {
				faker.FakeData(&name)

				lst = append(lst, name)
			}
		}()
	}

	wg.Wait()

	return
}

If you run this code with the -race flag as well you'll see where the race conditions are.

Thanks for the link, hopefully Go 2 solves this issue. I was able to get this running with the default source without any race conditions. Only issue is the randomness is decreased due to the seed always being the same.

from faker.

agoalofalife avatar agoalofalife commented on August 30, 2024

We can add methods from native package and set seed
rand.Seed(time.Now().UnixNano()) as suggested in the discussion.

I put this entry in the general function(FakeData)

func FakeData(a interface{}) error {
	rand.Seed(time.Now().UnixNano())
// ...
// excerpt
func randomString(n int) string {
	b := make([]byte, n)
	for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {

I did not notice that he did the same

 ZwbxVNpRjFBVyWfARJTCZRKqS WQJphBDMjQZmYkmrYVgHJWVkM ZrIWzJKnMQrxIUXAuvFaCZFHF zLTjnSfNjILIQfjsXnmakPEAa JExvPuGysQIoZnSlqlCdUGbat

@rdelval Do you have any suggestions?
You or I can send PR, just let us know)

from faker.

ridv avatar ridv commented on August 30, 2024

In this case, to keep the pseudo-randomess it's best to keep a single seed set at the beginning of the initialization and use the default source throughout the rest of the program.

I'll raise a PR and we can continue the discussion there.

Thanks for looking into this and being receptive to feedback!

from faker.

agoalofalife avatar agoalofalife commented on August 30, 2024

PR #23

from faker.

Related Issues (20)

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.