Skip to main content

SyncStream

A python tool for synchronizing the messages from different threads, processes, or hosts.

Installation

Run the following command for a basic installation:

pip install syncstream

Or the command for a full installation:

pip install syncstream[file,host]

Catch the stdout

The following codes provide an example of catching the python stdout:

from contextlib import redirect_stdoutimport syncstream
buffer = syncstream.LineBuffer(10)with redirect_stdout(buffer):  for i in range(20):      print(f'Message "{i:02d}".')  print('No line break.', end='')
messages = buffer.read()for mitem in messages:  print(mitem)

Catch the stdout of a sub-process

The following codes provide an example of catching the python stdout:

import multiprocessingfrom contextlib import redirect_stdoutimport syncstream

def worker_process(buffer):    '''Define the workder_process'''    try:        with redirect_stdout(buffer):            print('Message', 'item')    except Exception as err:        buffer.send_error(err)    else:        buffer.send_eof()

if __name__ == '__main__':    pbuf = syncstream.LineProcBuffer(10)    with multiprocessing.Pool(4) as pool:        pool.map_async(            worker_process,            tuple(pbuf.mirror for _ in range(4))        )        pbuf.wait()
    messages = pbuf.read()    for mitem in messages:        print(mitem)