| 159 | ) |
| 160 | @pytest.mark.parametrize("use_barrier", [True, False]) |
| 161 | def test_interrupt(use_barrier): |
| 162 | # Windows implements `queue.get` using polling, |
| 163 | # which means we can set an exception to interrupt the call to `get`. |
| 164 | # Python 3 on other platforms requires sending SIGINT to the main thread. |
| 165 | if sys.platform == "win32": |
| 166 | from _thread import interrupt_main |
| 167 | else: |
| 168 | main_thread = threading.get_ident() |
| 169 | |
| 170 | def interrupt_main() -> None: |
| 171 | signal.pthread_kill(main_thread, signal.SIGINT) |
| 172 | |
| 173 | barrier = threading.Barrier(20) if use_barrier else None |
| 174 | tasks_can_end = threading.Event() |
| 175 | |
| 176 | def f() -> None: |
| 177 | if barrier: |
| 178 | barrier.wait() |
| 179 | tasks_can_end.wait() |
| 180 | |
| 181 | def interrupt() -> None: |
| 182 | if barrier: |
| 183 | barrier.wait() |
| 184 | interrupt_main() |
| 185 | |
| 186 | dsk = {("f", i): (f,) for i in range(19)} |
| 187 | dsk["interrupt"] = (interrupt,) |
| 188 | |
| 189 | # Use explicitly created ThreadPoolExecutor to avoid leaking threads after |
| 190 | # the KeyboardInterrupt |
| 191 | with ThreadPoolExecutor(20) as pool: |
| 192 | with pytest.raises(KeyboardInterrupt): |
| 193 | get(dsk, list(dsk), pool=pool) |
| 194 | tasks_can_end.set() |
| 195 | |
| 196 | |
| 197 | def warm_up_thread_pool(num_workers: int | None) -> None: |