import time import threading import random import os import sys NTHREAD = 100 class MyThread(threading.Thread): def run(self): seconds = random.randint(0, 999) / 1000 # Call PyEval_SaveThread() to release the GIL time.sleep(seconds) # Call PyEval_RestoreThread() to acquire the GIL print(f"Start {NTHREAD} daemon threads", flush=True) threads = [MyThread(daemon=True) for _ in range(NTHREAD)] for thread in threads: thread.start() # Wait 10 ms until threads started time.sleep(0.1) print("Exit Python main thread", flush=True) # exit the main thread: # Py_Finalize() destroys the interpreter and thread states