##// END OF EJS Templates
Read each from stream until separator found or buffer is full
Garland Zhang -
Show More
@@ -4,6 +4,7 b''
4 # Distributed under the terms of the Modified BSD License.
4 # Distributed under the terms of the Modified BSD License.
5
5
6 import asyncio
6 import asyncio
7 import asyncio.exceptions
7 import atexit
8 import atexit
8 import errno
9 import errno
9 import os
10 import os
@@ -208,15 +209,23 b' class ScriptMagics(Magics):'
208 """Call a coroutine on the asyncio thread"""
209 """Call a coroutine on the asyncio thread"""
209 return asyncio.run_coroutine_threadsafe(coro, event_loop).result()
210 return asyncio.run_coroutine_threadsafe(coro, event_loop).result()
210
211
212 async def _readchunk(stream):
213 try:
214 return await stream.readuntil(b'\n')
215 except asyncio.exceptions.IncompleteReadError as e:
216 return e.partial
217 except asyncio.exceptions.LimitOverrunError as e:
218 return await stream.read(e.consumed)
219
211 async def _handle_stream(stream, stream_arg, file_object):
220 async def _handle_stream(stream, stream_arg, file_object):
212 while True:
221 while True:
213 line = (await stream.readline()).decode("utf8", errors="replace")
222 chunk = (await _readchunk(stream)).decode("utf8", errors="replace")
214 if not line:
223 if not chunk:
215 break
224 break
216 if stream_arg:
225 if stream_arg:
217 self.shell.user_ns[stream_arg] = line
226 self.shell.user_ns[stream_arg] = chunk
218 else:
227 else:
219 file_object.write(line)
228 file_object.write(chunk)
220 file_object.flush()
229 file_object.flush()
221
230
222 async def _stream_communicate(process, cell):
231 async def _stream_communicate(process, cell):
General Comments 0
You need to be logged in to leave comments. Login now