Last active
December 16, 2020 15:48
-
-
Save davipatti/6eb571c475927b92871c483246d71299 to your computer and use it in GitHub Desktop.
Getting my head around os.pipe in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
read, write = os.pipe() | |
# Variable 'parent' is the process id of the parent fork | |
# It gets a value of 0 when the parent process exits | |
parent = os.fork() | |
if parent: | |
# If write isn't closed, then when the child process | |
# never exits | |
os.close(write) | |
print("Parent reading") | |
read = os.fdopen(read) | |
print("Text = ", read.read()) | |
exit() | |
else: | |
os.close(read) | |
print("Child writing") | |
write = os.fdopen(write, "w") | |
write.write("Written by child") | |
print("Child closing") | |
write.close() | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment