Skip to content

Commit

Permalink
fix: auto hold don't work on script mode
Browse files Browse the repository at this point in the history
  • Loading branch information
wang0618 committed May 22, 2022
1 parent b4d8824 commit e60c313
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions pywebio/platform/path_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

LOCAL_STATIC_URL = '/_pywebio_static'


def filename_ok(f):
return not f.startswith(('.', '_'))

Expand Down
8 changes: 7 additions & 1 deletion pywebio/platform/tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ def start_server_in_current_thread_session():
class SingleSessionWSHandler(_webio_handler(cdn=False)):
session = None
instance = None
closed = False

def open(self):
self.main_session = False
Expand Down Expand Up @@ -426,6 +427,7 @@ def open(self):
def on_close(self):
if SingleSessionWSHandler.session is not None and self.main_session:
self.session.close()
self.closed = True
logger.debug('ScriptModeSession closed')

async def wait_to_stop_loop(server):
Expand All @@ -439,7 +441,11 @@ async def wait_to_stop_loop(server):
alive_none_daemonic_thread_cnt = sum(
1 for t in threading.enumerate() if t.is_alive() and not t.isDaemon()
)
await asyncio.sleep(1)
await asyncio.sleep(0.5)

if SingleSessionWSHandler.instance.session.need_keep_alive():
while not SingleSessionWSHandler.instance.closed:
await asyncio.sleep(0.5)

# 关闭Websocket连接
# Close the Websocket connection
Expand Down
8 changes: 5 additions & 3 deletions test/2.script_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ def test(server_proc: subprocess.Popen, browser: Chrome):

template.test_input(browser)

# script mode 下,此时 server 应停止
server_proc.wait(timeout=8)

time.sleep(1)
template.save_output(browser, '2.script_mode.html',
process_func=lambda i: i.replace('::1', '127.0.0.1')) # because tornado default bind ipv4 and ipv6 in script mode

browser.close()
time.sleep(3)

# script mode 下,此时 server 应停止
server_proc.wait(timeout=8)

if __name__ == '__main__':
# 设置监听端口,并关闭自动打开浏览器
Expand Down

0 comments on commit e60c313

Please sign in to comment.