1414import copy
1515import socket
1616import multiprocessing
17- import time
17+
1818
1919import signal
2020from urllib .parse import unquote
2121
2222from .rate_control import rate
2323
24- makeDaemonic = (platform .system () == "Windows" )
2524
2625# Redefine `Thread.run` to not show a traceback for Spyder when stopping
2726# the server by raising a KeyboardInterrupt or SystemExit.
@@ -39,8 +38,7 @@ def run(*args, **kwargs):
3938 try :
4039 run_old (* args , ** kwargs )
4140 except (KeyboardInterrupt , SystemExit ):
42- pass
43- # ("VPython server stopped.")
41+ print ("VPython server stopped." )
4442 except :
4543 raise
4644 threading .Thread .run = run
@@ -51,25 +49,22 @@ def run(*args, **kwargs):
5149
5250# Check for Ctrl+C. SIGINT will also be sent by our code if WServer is closed.
5351def signal_handler (signal , frame ):
54- #print("in signal handler, calling stop server")
5552 stop_server ()
5653
54+
5755signal .signal (signal .SIGINT , signal_handler )
5856
5957# Requests from client to http server can be the following:
6058# get glowcomm.html, library .js files, images, or font files
6159
6260
63- def find_free_port ():
61+ def find_free_port (port ):
6462 s = socket .socket ()
65- s .bind (('' , 0 )) # find an available port
63+ s .bind (('' , port ))
6664 return s .getsockname ()[1 ]
6765
68- if "VPYTHON_HTTP_PORT" in os .environ :
69- __HTTP_PORT = int (os .environ ["VPYTHON_HTTP_PORT" ])
70- else :
71- __HTTP_PORT = find_free_port ()
72- __SOCKET_PORT = find_free_port ()
66+ __HTTP_PORT = find_free_port (4200 )
67+ __SOCKET_PORT = find_free_port (4201 )
7368
7469try :
7570 if platform .python_implementation () == 'PyPy' :
@@ -78,17 +73,17 @@ def find_free_port():
7873except :
7974 pass
8075
81- # try: # machinery for reusing ports
82- # fd = open('free_ports')
83- # __HTTP_PORT = int(fd.readline())
84- # __SOCKET_PORT = int(fd.readline())
76+ # try: # machinery for reusing ports (2023/12/09 always use 4200 and 4201)
77+ # fd = open('free_ports')
78+ # __HTTP_PORT = int(fd.readline())
79+ # __SOCKET_PORT = int(fd.readline())
8580# except:
86- # __HTTP_PORT = find_free_port()
87- # __SOCKET_PORT = find_free_port()
88- # fd = open('free_ports', 'w') # this writes to user program's directory
89- # fd.write(str(__HTTP_PORT))
90- # fd.write('\n')
91- # fd.write(str(__SOCKET_PORT))
81+ # __HTTP_PORT = find_free_port()
82+ # __SOCKET_PORT = find_free_port()
83+ # fd = open('free_ports', 'w') # this writes to user program's directory
84+ # fd.write(str(__HTTP_PORT))
85+ # fd.write('\n')
86+ # fd.write(str(__SOCKET_PORT))
9287
9388# Make it possible for glowcomm.html to find out what the websocket port is:
9489js = __file__ .replace (
@@ -215,18 +210,8 @@ async def onMessage(self, data, isBinary):
215210 # message format used by notebook
216211 msg = {'content' : {'data' : [m ]}}
217212 loop = asyncio .get_event_loop ()
218- try :
219- await loop .run_in_executor (None , GW .handle_msg , msg )
220- except :
221- #
222- # this will throw a runtime exception after the main Thread
223- # has stopped, but we don't really case since the main thread
224- # is no longer there to do anything anyway.
225- if threading .main_thread ().is_alive ():
226- raise
227- else :
228- pass
229-
213+ await loop .run_in_executor (None , GW .handle_msg , msg )
214+
230215 def onClose (self , wasClean , code , reason ):
231216 """Called when browser tab is closed."""
232217 global websocketserving
@@ -261,24 +246,19 @@ def onClose(self, wasClean, code, reason):
261246
262247
263248try :
264- no_launch = os .environ .get ("VPYTHON_NO_LAUNCH_BROWSER" , False )
265- if no_launch == "0" :
266- no_launch = False
267249 if platform .python_implementation () == 'PyPy' :
268250 server_address = ('' , 0 ) # let HTTPServer choose a free port
269251 __server = HTTPServer (server_address , serveHTTP )
270252 port = __server .server_port # get the chosen port
271253 # Change the global variable to store the actual port used
272254 __HTTP_PORT = port
273- if not no_launch :
274- _webbrowser .open ('http://localhost:{}' .format (port )
255+ _webbrowser .open ('http://localhost:{}' .format (port )
275256 ) # or webbrowser.open_new_tab()
276257 else :
277258 __server = HTTPServer (('' , __HTTP_PORT ), serveHTTP )
278259 # or webbrowser.open_new_tab()
279- if not no_launch :
280- if _browsertype == 'default' : # uses default browser
281- _webbrowser .open ('http://localhost:{}' .format (__HTTP_PORT ))
260+ if _browsertype == 'default' : # uses default browser
261+ _webbrowser .open ('http://localhost:{}' .format (__HTTP_PORT ))
282262
283263except :
284264 pass
@@ -312,7 +292,7 @@ def start_Qapp(port):
312292 __m = multiprocessing .Process (target = start_Qapp , args = (__HTTP_PORT ,))
313293 __m .start ()
314294
315- __w = threading .Thread (target = __server .serve_forever , daemon = makeDaemonic )
295+ __w = threading .Thread (target = __server .serve_forever )
316296__w .start ()
317297
318298
@@ -345,16 +325,14 @@ def start_websocket_server():
345325# Put the websocket server in a separate thread running its own event loop.
346326# That works even if some other program (e.g. spyder) already running an
347327# async event loop.
348- __t = threading .Thread (target = start_websocket_server , daemon = makeDaemonic )
328+ __t = threading .Thread (target = start_websocket_server )
349329__t .start ()
350330
351331
352332def stop_server ():
353333 """Shuts down all threads and exits cleanly."""
354- #print("in stop server")
355334 global __server
356335 __server .shutdown ()
357-
358336 event_loop = txaio .config .loop
359337 event_loop .stop ()
360338 # We've told the event loop to stop, but it won't shut down until we poke
@@ -373,36 +351,19 @@ def stop_server():
373351 raise KeyboardInterrupt
374352
375353 if threading .main_thread ().is_alive ():
376- #print("main is alive...")
377354 sys .exit (0 )
378355 else :
379- #
380- # check to see if the event loop is still going, if so join it.
381- #
382- #print("main is dead..")
383- if __t .is_alive ():
384- #print("__t is alive still")
385- if threading .get_ident () != __t .ident :
386- #print("but it's not my thread, so I'll join...")
387- __t .join ()
388- else :
389- #print("__t is alive, but that's my thread! So skip it.")
390- pass
391- else :
392- if makeDaemonic :
393- sys .exit (0 )
394-
356+ pass
395357 # If the main thread has already stopped, the python interpreter
396358 # is likely just running .join on the two remaining threads (in
397359 # python/threading.py:_shutdown). Since we just stopped those threads,
398360 # we'll now exit.
399-
361+
362+
400363GW = GlowWidget ()
401364
402365while not (httpserving and websocketserving ): # try to make sure setup is complete
403- time .sleep (0.1 )
404-
366+ rate (60 )
405367
406368# Dummy variable to import
407369_ = None
408-
0 commit comments