BruceSherwood / vpython-jupyter

This repository has been moved to https://github.com/vpython/vpython-jupyter
64 stars 33 forks source link

server_stop() - RuntimeError: Event loop is closed #148

Open SadSack963 opened 2 years ago

SadSack963 commented 2 years ago

Firstly, my compliments to everyone involved on an easily implemented and fast 3D animation package for my favourite programming language.

vpython7 version:   7.6.2
Python version:     3.9.6
IDE:                PyCharm 2021.2 (Community Edition)
OS:                 Windows 10 version 20H2
Browser:            Firefox version 93.0

My test program:

from vpython import *
from vpython.no_notebook import stop_server

def shutdown(keyup_event):
    if keyup_event.key == 'x':
        print('You pressed "x"')
        global running
        running = False
        stop_server()

box(
    pos=vector(0, 0, 0),
    size=vector(1, 1, 1),
    color=color.gray(1),
    opacity=0.1
)

scene.bind('keyup', shutdown)
running = True

while running:
    rate(30)

The web page does close and the program stops, but I still get an error.

E:\Python\Projects\3D-vpython\venv_9\Scripts\python.exe E:/Python/Projects/3D-vpython/test_02.py
You pressed "x"
Task was destroyed but it is pending!
task: <Task pending name='Task-43' coro=<WSserver.onMessage() running at E:\Python\Projects\3D-vpython\venv_9\lib\site-packages\vpython\no_notebook.py:214> wait_for=<Future finished result=None>>
Task was destroyed but it is pending!
task: <Task pending name='Task-4' coro=<IocpProactor.accept.<locals>.accept_coro() running at D:\Users\John\AppData\Local\Programs\Python\Python39\lib\asyncio\windows_events.py:566> wait_for=<_OverlappedFuture cancelled>>
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000018339B28700>
Traceback (most recent call last):
  File "D:\Users\John\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 116, in __del__
  File "D:\Users\John\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close
  File "D:\Users\John\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 746, in call_soon
  File "D:\Users\John\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
RuntimeError: Event loop is closed

Process finished with exit code 0

It seems that stop_server() doesn't complete because if I change the sequence to

def shutdown(keyup_event):
    if keyup_event.key == 'x':
        print('You pressed "x"')
        stop_server()
        global running
        running = False

then the browser page remains open and the program does not stop.

I have just tried this solution: sfaleron's solution and it works for me too.