Skip to content

Commit ee5e673

Browse files
committed
try daemonic threads on windows
1 parent ca5f229 commit ee5e673

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

vpython/no_notebook.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from .rate_control import rate
2222

23+
makeDaemonic = (platform.system() == "Windows")
2324

2425
# Redefine `Thread.run` to not show a traceback for Spyder when stopping
2526
# the server by raising a KeyboardInterrupt or SystemExit.
@@ -302,7 +303,7 @@ def start_Qapp(port):
302303
__m = multiprocessing.Process(target=start_Qapp, args=(__HTTP_PORT,))
303304
__m.start()
304305

305-
__w = threading.Thread(target=__server.serve_forever)
306+
__w = threading.Thread(target=__server.serve_forever, daemon=makeDaemonic)
306307
__w.start()
307308

308309

@@ -335,12 +336,13 @@ def start_websocket_server():
335336
# Put the websocket server in a separate thread running its own event loop.
336337
# That works even if some other program (e.g. spyder) already running an
337338
# async event loop.
338-
__t = threading.Thread(target=start_websocket_server)
339+
__t = threading.Thread(target=start_websocket_server, daemon=makeDaemonic)
339340
__t.start()
340341

341342

342343
def stop_server():
343344
"""Shuts down all threads and exits cleanly."""
345+
print("in stop server")
344346
global __server
345347
__server.shutdown()
346348

@@ -362,18 +364,23 @@ def stop_server():
362364
raise KeyboardInterrupt
363365

364366
if threading.main_thread().is_alive():
367+
print("main is alive...")
365368
sys.exit(0)
366369
else:
367370
#
368371
# check to see if the event loop is still going, if so join it.
369372
#
373+
print("main is dead..")
370374
if __t.is_alive():
371375
print("__t is alive still")
372376
if threading.get_ident() != __t.ident:
373377
print("but it's not my thread, so I'll join...")
374378
__t.join()
375379
else:
376380
print("__t is alive, but that's my thread! So skip it.")
381+
else:
382+
if makeDaemonic:
383+
sys.exit(0)
377384

378385
# If the main thread has already stopped, the python interpreter
379386
# is likely just running .join on the two remaining threads (in

vpython/vpython.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@
1515
from ._notebook_helpers import _isnotebook
1616
from ._vector_import_helper import (vector, mag, norm, cross, dot, adjust_up,
1717
adjust_axis, object_rotate)
18+
19+
20+
def Exit():
21+
print("in atexit")
22+
while True:
23+
rate(60)
24+
25+
import atexit
26+
27+
if platform.system() == 'Windows':
28+
atexit.register(Exit)
1829

1930
# List of names that will be imported from this file with import *
2031
__all__ = ['Camera', 'GlowWidget', 'version', 'GSversion', 'Mouse', 'arrow', 'attach_arrow',

0 commit comments

Comments
 (0)