forked from platypush/platypush
[shell
] Better buffering for the output sent to websockets.
This commit is contained in:
parent
a652bd9df8
commit
81e99a0e22
1 changed files with 21 additions and 8 deletions
|
@ -1,7 +1,8 @@
|
||||||
from base64 import b64encode
|
from base64 import b64encode
|
||||||
import json
|
import json
|
||||||
import threading
|
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
from typing import Callable, Optional
|
from typing import Callable, Optional
|
||||||
from uuid import uuid1
|
from uuid import uuid1
|
||||||
|
|
||||||
|
@ -54,15 +55,27 @@ class ShellPlugin(Plugin):
|
||||||
shell=True,
|
shell=True,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
) as proc:
|
) as proc:
|
||||||
while proc.poll() is None:
|
first_loop = True
|
||||||
if not proc.stdout:
|
|
||||||
|
while proc.poll() is None and proc.stdout and proc.stdout.readable():
|
||||||
|
buf = b''
|
||||||
|
bufsize = 1024
|
||||||
|
|
||||||
|
while len(buf) < bufsize:
|
||||||
|
ch = proc.stdout.read(1)
|
||||||
|
if not ch:
|
||||||
break
|
break
|
||||||
|
|
||||||
while True:
|
buf += ch
|
||||||
buf = proc.stdout.read(1024)
|
if ch in (b'\r', b'\n'):
|
||||||
if not buf:
|
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if first_loop:
|
||||||
|
first_loop = False
|
||||||
|
# An artificial delay to let the client connect to the websocket,
|
||||||
|
# otherwise the first few lines of output might be lost.
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
self._send_ws_output(cmd_id, buf)
|
self._send_ws_output(cmd_id, buf)
|
||||||
finally:
|
finally:
|
||||||
self._send_ws_output(cmd_id, None)
|
self._send_ws_output(cmd_id, None)
|
||||||
|
|
Loading…
Reference in a new issue