I have created a small test case using pytest to demonstrate the issue:
from threading import Thread, get_ident
def test_threading():
threads = []
ids = []
def append_id():
# time.sleep(1)
ids.append(get_ident())
for _ in range(5):
thread = Thread(target=append_id)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
assert len(set(ids)) == 5
The test is failing because get_ident returns the same ID for different threads. But when I add time.sleep(1) in each thread, the test passes.
I'm not sure I understand why.
I'm using Python 3.9.0 and Pytest 7.1.2.