Skip to content

Instantly share code, notes, and snippets.

@davipatti
Last active December 16, 2020 15:48
Show Gist options
  • Save davipatti/6eb571c475927b92871c483246d71299 to your computer and use it in GitHub Desktop.
Save davipatti/6eb571c475927b92871c483246d71299 to your computer and use it in GitHub Desktop.
Getting my head around os.pipe in python
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