@@ -1045,20 +1045,24 @@ Low-level REPL communication. Usually accessed via `mpy.comm`.
10451045
10461046### Methods
10471047
1048- #### exec(command, timeout=5 )
1048+ #### exec(command, timeout=None, stream=False )
10491049
10501050Execute Python command on device.
10511051
10521052``` python
1053- mpy.comm.exec(command, timeout = 5 )
1053+ mpy.comm.exec(command, timeout = None , stream = False )
10541054```
10551055
10561056** Parameters:**
10571057- ` command ` (str): Python code to execute
1058- - ` timeout ` (int): Maximum wait time in seconds. ` 0 ` = submit only (send code, don't wait for output)
1058+ - ` timeout ` (float|None): Total wall time in seconds. ` None ` = wait forever, ` 0 ` = submit only (fire-and-forget)
1059+ - ` stream ` : Output mode:
1060+ - ` False ` (default): Return all output as ` bytes `
1061+ - ` True ` : Return a generator yielding ` bytes ` chunks
1062+ - file-like object: Write chunks to it (supports both binary and text streams via ` io.TextIOBase ` detection), return ` b'' `
10591063
10601064** Returns:**
1061- - ` bytes ` : Command stdout output ( ` b'' ` when timeout=0)
1065+ - ` bytes ` , generator, or ` b'' ` ( when stream is file-like or timeout=0)
10621066
10631067** Example:**
10641068``` python
@@ -1068,6 +1072,15 @@ b'Hello\r\n'
10681072>> > mpy.comm.exec(" import sys" )
10691073b ' '
10701074
1075+ # Streaming output (generator)
1076+ >> > for chunk in mpy.comm.exec(" print('Hello')" , stream = True ):
1077+ ... print (chunk)
1078+
1079+ # Stream to stdout
1080+ >> > import sys
1081+ >> > mpy.comm.exec(" print('Hello')" , stream = sys.stdout.buffer)
1082+ b ' '
1083+
10711084# Submit code without waiting for output (fire-and-forget)
10721085>> > mpy.comm.exec(" while True: print('tick')" , timeout = 0 )
10731086b ' '
@@ -1132,23 +1145,24 @@ mpy.comm.enter_raw_repl()
11321145mpy.comm.exit_raw_repl()
11331146```
11341147
1135- #### exec_raw_paste(command, timeout=5)
1148+ #### exec_raw_paste(command, timeout=5, stream=False )
11361149
11371150Execute Python command using raw-paste mode with flow control.
11381151
11391152Raw-paste mode compiles code as it receives it, using less RAM and providing
11401153better reliability for large code transfers. Requires MicroPython 1.17+.
11411154
11421155``` python
1143- mpy.comm.exec_raw_paste(command, timeout = 5 )
1156+ mpy.comm.exec_raw_paste(command, timeout = 5 , stream = False )
11441157```
11451158
11461159** Parameters:**
11471160- ` command ` (str or bytes): Python code to execute
1148- - ` timeout ` (int): Maximum wait time in seconds. ` 0 ` = submit only (send code, don't wait for output)
1161+ - ` timeout ` (float|None): Total wall time in seconds. ` 0 ` = submit only (fire-and-forget)
1162+ - ` stream ` : Output mode (same as ` exec() ` : ` False ` /` True ` /file-like object)
11491163
11501164** Returns:**
1151- - ` bytes ` : Command stdout output ( ` b'' ` when timeout=0 )
1165+ - ` bytes ` , generator, or ` b'' ` (depending on ` stream ` and ` timeout ` )
11521166
11531167** Example:**
11541168``` python
@@ -1165,20 +1179,21 @@ b'99\r\n'
11651179- ` MpyError ` : If raw-paste mode is not supported by device
11661180- ` CmdError ` : If command raises an exception on device
11671181
1168- #### try_raw_paste(command, timeout=5)
1182+ #### try_raw_paste(command, timeout=5, stream=False )
11691183
11701184Try raw-paste mode, fall back to regular exec if not supported.
11711185
11721186``` python
1173- mpy.comm.try_raw_paste(command, timeout = 5 )
1187+ mpy.comm.try_raw_paste(command, timeout = 5 , stream = False )
11741188```
11751189
11761190** Parameters:**
11771191- ` command ` (str or bytes): Python code to execute
1178- - ` timeout ` (int): Maximum wait time in seconds. ` 0 ` = submit only (send code, don't wait for output)
1192+ - ` timeout ` (float|None): Total wall time in seconds. ` 0 ` = submit only (fire-and-forget)
1193+ - ` stream ` : Output mode (same as ` exec() ` : ` False ` /` True ` /file-like object)
11791194
11801195** Returns:**
1181- - ` bytes ` : Command stdout output ( ` b'' ` when timeout=0 )
1196+ - ` bytes ` , generator, or ` b'' ` (depending on ` stream ` and ` timeout ` )
11821197
11831198** Example:**
11841199``` python
@@ -1191,6 +1206,37 @@ This method automatically detects if raw-paste mode is supported and caches
11911206the result. On older MicroPython versions, it silently falls back to regular
11921207` exec() ` .
11931208
1209+ #### monitor(stream=False, follow=False)
1210+
1211+ Monitor device output in normal REPL mode.
1212+
1213+ By default, reads output until REPL prompt (` >>> ` ) is detected (program finished).
1214+ With ` follow=True ` , reads indefinitely (Ctrl-C to stop).
1215+
1216+ ``` python
1217+ mpy.comm.monitor(stream = False , follow = False )
1218+ ```
1219+
1220+ ** Parameters:**
1221+ - ` stream ` : Output mode (same as ` exec() ` : ` False ` /` True ` /file-like object)
1222+ - ` follow ` (bool): If ` True ` , don't stop at REPL prompt
1223+
1224+ ** Returns:**
1225+ - ` bytes ` , generator, or ` b'' ` (depending on ` stream ` )
1226+
1227+ ** Example:**
1228+ ``` python
1229+ # Capture output until program ends
1230+ >> > output = mpy.comm.monitor()
1231+
1232+ # Stream to stdout
1233+ >> > import sys
1234+ >> > mpy.comm.monitor(stream = sys.stdout.buffer)
1235+
1236+ # Follow continuously
1237+ >> > mpy.comm.monitor(stream = sys.stdout.buffer, follow = True )
1238+ ```
1239+
11941240## Exception Classes
11951241
11961242### MpyError
0 commit comments