Streams
shtk.Stream instances are pairs of file-like objects (one for reading data from the process, one for writing data to the process) used for communication with running processes. If a stream is one way (e.g. FileStream) then the underlying file-like objects reader or writer may be handles to os.devnull.
shtk.Stream instances are usually constructed internally within SHTK, rather than being directly instantiated by the end user.
shtk.Stream
- class shtk.Stream(fileobj_r=None, fileobj_w=None)
Bases:
object
Base class for other Stream classes.
Wraps file-like objects to couple readers and writers to the same streams (where it makes sense) and more tightly control closure of the stream. Also functions as a context manager (yielding self) that calls self.close() upon exit.
- Parameters
fileobj_r (file-like or None) – A file-like object suitable for reading.
fileobj_w (file-like or None) – A file-like object suitable for writing.
- fileobj_r
A file-like object suitable for reading.
- Type
file-like or None
- fileobj_w
A file-like object suitable for writing.
- Type
file-like or None
- reader()
Returns fileobj_r
- Returns
self.fileobj_r
- Return type
file-like
- writer()
Returns fileobj_w
- Returns
self.fileobj_w
- Return type
file-like
- close_reader()
Closes self.fileobj_r if it’s not None, then set it to None
- close_writer()
Closes self.fileobj_w if it’s not None, then set it to None
- close()
Calls self.close_reader() and self.close_writer()
shtk.FileStream
- class shtk.FileStream(path, mode, user=None, group=None)
Bases:
Stream
Opens a file for reading or writing
- Parameters
path (str or pathlib.Path) – The path of the file to open.
mode (str) – Mode passed to open() when opening the file. If mode contains ‘r’ then the file will be opened for reading. If the mode contains ‘w’ or ‘a’ it will be opened for writing.
user (None, int, str) – The user that will own the file (if ‘w’ in mode). If user is an int, the file will be chown’d to the user whose uid=user. If user is an str, the file will be chown’d to the user whose name=user.
group (None, int, str) – The group that will own the file (if ‘w’ in mode). If group is an int, the file will be chown’d to the group whose gid=group. If group is an str, the file will be chown’d to the group whose name=group.
- close()
Calls self.close_reader() and self.close_writer()
- close_reader()
Closes self.fileobj_r if it’s not None, then set it to None
- close_writer()
Closes self.fileobj_w if it’s not None, then set it to None
- reader()
Returns fileobj_r
- Returns
self.fileobj_r
- Return type
file-like
- writer()
Returns fileobj_w
- Returns
self.fileobj_w
- Return type
file-like
shtk.ManualStream
- class shtk.ManualStream(fileobj_r=None, fileobj_w=None)
Bases:
Stream
Uses provided file-like objects for fileobj_r and fileobj_w.
Note
The files will not be manually closed even when close_reader() or close_writer() are called. Closing the files is the responsibility of the caller.
- Parameters
fileobj_r (file-like) – The file-like object to use for self.fileobj_r.
fileobj_w (file-like) – The file-like object to use for self.fileobj_w.
- close_r
Whether the reader should be closed when close_reader() is called.
- Type
boolean
- close_w
Whether the writer should be closed when close_writer() is called.
- Type
boolean
- close_reader()
Close the reader only if it wasn’t provided at instantiation.
- close_writer()
Close the writer only if it wasn’t provided at instantiation.
- close()
Calls self.close_reader() and self.close_writer()
- reader()
Returns fileobj_r
- Returns
self.fileobj_r
- Return type
file-like
- writer()
Returns fileobj_w
- Returns
self.fileobj_w
- Return type
file-like
shtk.NullStream
- class shtk.NullStream
Bases:
Stream
Opens os.devnull for both reading and writing
- close()
Calls self.close_reader() and self.close_writer()
- close_reader()
Closes self.fileobj_r if it’s not None, then set it to None
- close_writer()
Closes self.fileobj_w if it’s not None, then set it to None
- reader()
Returns fileobj_r
- Returns
self.fileobj_r
- Return type
file-like
- writer()
Returns fileobj_w
- Returns
self.fileobj_w
- Return type
file-like
shtk.PipeStream
- class shtk.PipeStream(binary=False, flags=0)
Bases:
Stream
Creates an os.pipe2() suitable for communicating between processes
- Parameters
binary (boolean) – Whether the streams should be opened in binary mode (Default value = False).
flags (int) – Flags to pass to os.pipe2 in addition to os.O_CLOEXEC (Default value = 0).
user (None, int, str) – The user that will own the pipe. If user is an int, the file will be chown’d to the user whose uid=user. If user is an str, the file will be chown’d to the user whose name=user.
group (None, int, str) – The group that will own the pipe. If group is an int, the file will be chown’d to the group whose gid=group. If group is an str, the file will be chown’d to the group whose name=group.
- close()
Calls self.close_reader() and self.close_writer()
- close_reader()
Closes self.fileobj_r if it’s not None, then set it to None
- close_writer()
Closes self.fileobj_w if it’s not None, then set it to None
- reader()
Returns fileobj_r
- Returns
self.fileobj_r
- Return type
file-like
- writer()
Returns fileobj_w
- Returns
self.fileobj_w
- Return type
file-like