Wait until the Python thread state of all non-daemon threads get deleted.
()
| 1539 | _main_thread = _MainThread() |
| 1540 | |
| 1541 | def _shutdown(): |
| 1542 | """ |
| 1543 | Wait until the Python thread state of all non-daemon threads get deleted. |
| 1544 | """ |
| 1545 | # Obscure: other threads may be waiting to join _main_thread. That's |
| 1546 | # dubious, but some code does it. We can't wait for C code to release |
| 1547 | # the main thread's tstate_lock - that won't happen until the interpreter |
| 1548 | # is nearly dead. So we release it here. Note that just calling _stop() |
| 1549 | # isn't enough: other threads may already be waiting on _tstate_lock. |
| 1550 | if _main_thread._is_stopped: |
| 1551 | # _shutdown() was already called |
| 1552 | return |
| 1553 | |
| 1554 | global _SHUTTING_DOWN |
| 1555 | _SHUTTING_DOWN = True |
| 1556 | |
| 1557 | # Call registered threading atexit functions before threads are joined. |
| 1558 | # Order is reversed, similar to atexit. |
| 1559 | for atexit_call in reversed(_threading_atexits): |
| 1560 | atexit_call() |
| 1561 | |
| 1562 | # Main thread |
| 1563 | if _main_thread.ident == get_ident(): |
| 1564 | tlock = _main_thread._tstate_lock |
| 1565 | # The main thread isn't finished yet, so its thread state lock can't |
| 1566 | # have been released. |
| 1567 | assert tlock is not None |
| 1568 | assert tlock.locked() |
| 1569 | tlock.release() |
| 1570 | _main_thread._stop() |
| 1571 | else: |
| 1572 | # bpo-1596321: _shutdown() must be called in the main thread. |
| 1573 | # If the threading module was not imported by the main thread, |
| 1574 | # _main_thread is the thread which imported the threading module. |
| 1575 | # In this case, ignore _main_thread, similar behavior than for threads |
| 1576 | # spawned by C libraries or using _thread.start_new_thread(). |
| 1577 | pass |
| 1578 | |
| 1579 | # Join all non-deamon threads |
| 1580 | while True: |
| 1581 | with _shutdown_locks_lock: |
| 1582 | locks = list(_shutdown_locks) |
| 1583 | _shutdown_locks.clear() |
| 1584 | |
| 1585 | if not locks: |
| 1586 | break |
| 1587 | |
| 1588 | for lock in locks: |
| 1589 | # mimic Thread.join() |
| 1590 | lock.acquire() |
| 1591 | lock.release() |
| 1592 | |
| 1593 | # new threads can be spawned while we were waiting for the other |
| 1594 | # threads to complete |
| 1595 | |
| 1596 | |
| 1597 | def main_thread(): |