current problems in UbuntuExpress

kamstrup dlist at ubuntuforums.org
Wed Aug 17 05:20:13 CDT 2005


Just to clear things up, here's a version that uses processes instead:


Code:
--------------------
    #!/usr/bin/env python

  #

  #

  # Pipes example - use for what ever, and in any way, 

  # you like.

  # Demonstrates the use of a pipe between two

  # processes.

  

  import os, sys

  import fcntl

  import time

  import random

  from threading import Thread

  

  def messenger_process (write_fd):

  	"""Does non-blocking writes to a pipe"""

  	# Set up write_fd for non-blocking writes

  	flags = fcntl.fcntl (write_fd, fcntl.F_GETFL, 0)

  	flags = flags  | os.O_NONBLOCK

  	fcntl.fcntl (write_fd, fcntl.F_SETFL, flags)

  		

  	for x in "abcdefg":

  		time.sleep (random.random())

  		os.write (write_fd, x)

  		print "wrote: %s" % x

  	os.close (write_fd)

  	print "End of messenger"

  

  		

  def reciever_process (read_fd):

  	"""Reads stuff from a pipe and outputs them"""

  	while 1:

  		letter = os.read (read_fd, 1)

  		if letter != "g": print "read: %s" % letter

  		else:

  			print "read: %s" % letter

  			break

  	print "End of reciever"

  	os.close (read_fd)

  	sys.exit (0)

  

  if __name__ == "__main__":

  	# create a pipe

  	read_fd, write_fd = os.pipe()

  

  	# fork another process

  	# each process will be responsible for closing

  	# its own file descriptor	

  	pid = os.fork ()

  	if pid > 0:

  		# The parent process becomes the messenger

  		messenger_process (write_fd)

  	else:

  		reciever_process (read_fd)

  	
--------------------


-- 
kamstrup



More information about the ubuntu-devel mailing list