python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.32k stars 2.23k forks source link

Added running_in_another_thread() #8518

Closed radarhere closed 5 days ago

radarhere commented 2 weeks ago

Possible way to help #8492. Alternative to #8501

I've created a Tests/helper.py function called running_in_another_thread() that uses a global variable and inspect to determine if a test has already started running in another thread, so that we can return early if we would only like the test to run in one thread at a time.

This is used here in two cases

  1. 8492 has found that running a test that calls register_handler at the beginning, and removes the handler at the end, fails when the test is run across multiple threads. My thinking is that if it ok for register_handler to operate across threads, and that it doesn't need to register multiple handlers in parallel. So this PR returns early from the relevant test if it is already running in another thread.

  2. https://github.com/tonybaloney/pytest-freethreaded/issues/10 finds an error when running https://github.com/python-pillow/Pillow/blob/16372dd951c5f7ab664286e6ad25b2e7ed645358/Tests/test_core_resources.py#L50-L54 across multiple threads. I again think that Image.core.set_alignment doesn't need to be run across multiple threads simultaneously, so this PR returns early.