2

The following code shuffles a large array of data into the exact same order in three different threads. Why is it not uniquely shuffling them?

Settings = json.loads(open('Settings.json', 'r').read())

def update_assets():
        Array = Settings['Location']
        random.shuffle(Array)
        print(Array)

for x in range(3):
    threading.Thread(target = update_assets).start()
Menace
  • 207
  • 1
  • 3
  • 11
  • Does this answer your question? [Variable passed to multiple threads of the same function](https://stackoverflow.com/questions/58309718/variable-passed-to-multiple-threads-of-the-same-function) – Peter O. Jan 12 '20 at 01:26

2 Answers2

3

All the three threads are shuffling the same object. You MAY see different results depending on how the threads gets scheduled, but all threads are shuffling the same array and printing it.

If you want to see three different results you should make three copies, for example with:

def update_assets(locs):
    random.shuffle(locs)
    print(locs)

for x in range(3):
    threading.Thread(target = update_assets,
                     args = (Settings['Location'][:],)).start()
6502
  • 112,025
  • 15
  • 165
  • 265
1

random.shuffle uses a global random generator instance which is shared across all threads. However, Python's RNG appears to be thread safe, so this can't explain the whole problem.

Another aspect of the problem is that Array is likewise global data shared across all threads. But the shuffle (which happens in-place) and the print-out of this array are not synchronized between the threads, leading to race conditions (when two threads have a different view of Array's contents).

To solve this issue, make a copy of Array in each process before shuffling it in that process — remember that shuffle does an in-place shuffle of its contents.

Peter O.
  • 32,158
  • 14
  • 82
  • 96