Why does Faker return the same name when used in a multiprocessing pool in WSL2?

Viewed 20

I am using Faker 14.2.0 to generate names in a Python 3.9.2 multiprocessing.Pool, but I don't get a unique name every time.

import multiprocessing

from faker import Faker

fake = Faker()
lock = multiprocessing.Lock()

def print_name(index):
    lock.acquire()
    print(index, fake.name())
    lock.release()

if __name__ == '__main__':
    with multiprocessing.Pool() as pool:
        for i in range(10):
            pool.apply_async(print_name, args=(i, ))
        pool.close()
        pool.join()

The output shows the unique index, but mostly duplicate names:

0 Jacob Hughes
5 Jacob Hughes
2 Jacob Hughes
3 Jacob Hughes
4 Jacob Hughes
1 Jacob Hughes
6 Jacob Hughes
8 Kristi Herrera
7 Jacob Hughes
9 Kristi Herrera

The expected behavior in the REPL:

>>> from faker import Faker
>>> fake = Faker()
>>> fake.name()
'Christian Simpson'
>>> fake.name()
'Timothy Singh'
>>> fake.name()
'Michael Fletcher'

If this is a synchronization problem, shouldn't the Lock solve that?

Edit: thas is happening in Windows Subsystem for Linux 2, with both Debian GNU/Linux 11 (bullseye) and Ubuntu 20.04.5 LTS.

0 Answers
Related