Skip to content

Commit 3169423

Browse files
committed
Write a comment about the necessity of using a separate thread
1 parent da5f9ea commit 3169423

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/xopen/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,15 @@ def __init__( # noqa: C901
249249
assert self.process.stdin is not None
250250
if "r" in mode:
251251
self.in_pipe = self.process.stdin
252+
# A python subprocess can read and write from pipes, but not from
253+
# Python in-memory objects. In order for a program to read from an
254+
# in-memory object, a pipe must be created. This pipe must be fed
255+
# data from the in-memory object. This must be done in a separate
256+
# thread, because IO operations will block when the pipe is full
257+
# when writing, or empty when reading. Since the quantity of output
258+
# data generated by a certain amount of input data is unknown, the
259+
# only way to prevent a blocking application is to write
260+
# data continuously to the process stdin on another thread.
252261
self.in_thread = threading.Thread(target=self._feed_pipe)
253262
self.in_thread.start()
254263
self._file: BinaryIO = self.process.stdout # type: ignore

0 commit comments

Comments
 (0)