import io
import threading
import time
from mrrc import MARCReader
def test_gil_release():
with open('tests/data/fixtures/10k_records.mrc', 'rb') as f:
fixture = f.read()
parsing_started = threading.Event()
other_thread_ran = threading.Event()
def read_with_notification():
reader = MARCReader(io.BytesIO(fixture[:1000000])) count = 0
for record in reader:
count += 1
if count == 100:
parsing_started.set()
if count > 200:
break
return count
def other_work():
parsing_started.wait(timeout=10)
start_time = time.perf_counter()
time.sleep(0.001) end_time = time.perf_counter()
other_thread_ran.set()
return end_time - start_time
main_thread = threading.Thread(target=read_with_notification)
main_thread.start()
other_thread = threading.Thread(target=other_work)
other_thread.start()
main_thread.join(timeout=30)
other_thread.join(timeout=30)
if other_thread_ran.is_set():
print("✅ GIL appears to be released (other thread ran)")
else:
print("❌ GIL appears to NOT be released (other thread blocked)")
if __name__ == "__main__":
test_gil_release()