Skip to content

Commit 3313945

Browse files
committed
fix: auto hold don't work on script mode
1 parent b4d8824 commit 3313945

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

pywebio/platform/path_deploy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
LOCAL_STATIC_URL = '/_pywebio_static'
2121

22+
2223
def filename_ok(f):
2324
return not f.startswith(('.', '_'))
2425

pywebio/platform/tornado.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ def start_server_in_current_thread_session():
397397
class SingleSessionWSHandler(_webio_handler(cdn=False)):
398398
session = None
399399
instance = None
400+
closed = False
400401

401402
def open(self):
402403
self.main_session = False
@@ -426,6 +427,7 @@ def open(self):
426427
def on_close(self):
427428
if SingleSessionWSHandler.session is not None and self.main_session:
428429
self.session.close()
430+
self.closed = True
429431
logger.debug('ScriptModeSession closed')
430432

431433
async def wait_to_stop_loop(server):
@@ -439,7 +441,11 @@ async def wait_to_stop_loop(server):
439441
alive_none_daemonic_thread_cnt = sum(
440442
1 for t in threading.enumerate() if t.is_alive() and not t.isDaemon()
441443
)
442-
await asyncio.sleep(1)
444+
await asyncio.sleep(0.5)
445+
446+
if SingleSessionWSHandler.instance.session.need_keep_alive():
447+
while not SingleSessionWSHandler.instance.closed:
448+
await asyncio.sleep(0.5)
443449

444450
# 关闭Websocket连接
445451
# Close the Websocket connection

test/2.script_mode.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ def test(server_proc: subprocess.Popen, browser: Chrome):
2626

2727
template.test_input(browser)
2828

29-
# script mode 下,此时 server 应停止
30-
server_proc.wait(timeout=8)
31-
3229
time.sleep(1)
3330
template.save_output(browser, '2.script_mode.html',
34-
process_func=lambda i: i.replace('::1', '127.0.0.1')) # because tornado default bind ipv4 and ipv6 in script mode
31+
# because tornado default bind ipv4 and ipv6 in script mode
32+
process_func=lambda i: i.replace('::1', '127.0.0.1'))
33+
34+
browser.close()
35+
time.sleep(3)
36+
37+
# script mode 下,此时 server 应停止
38+
server_proc.wait(timeout=8)
3539

3640

3741
if __name__ == '__main__':

0 commit comments

Comments
 (0)