Advertisement
rfmonk

IPC_socketpair.py

Jun 26th, 2014
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # performing basic inter process comms
  4. # using connected sockets or socketpair
  5. #
  6.  
  7.  
  8. import socket
  9. import os
  10.  
  11. BUFSIZE = 1024
  12.  
  13. def test_socketpair():
  14.     parent, child = socket.socketpair()
  15.  
  16.     pid = os.fork()
  17.     try:
  18.         if pid:
  19.             print "@Parent, sending message..."
  20.             child.close()
  21.             parent.sendall("Hello from parent!")
  22.             response = parent.recv(BUFSIZE)
  23.             print "Response from child:", response
  24.             parent.close()
  25.  
  26.         else:
  27.             print "@Child, waiting for message from parent"
  28.             parent.close()
  29.             message = child.recv(BUFSIZE)
  30.             print "Message from parent:", message
  31.             child.sendall("Hello from child!!")
  32.             child.close()
  33.     except Exception, err:
  34.         print "Error: %s" %err
  35.  
  36. if __name__ == '__main__':
  37.     test_socketpair()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement