0
import cgi

def fill():
   s = """\
<html><body>
<form method="get" action="./show">
<p>Type a word: <input type="text" name="word">
<input type="submit" value="Submit"</p>
</form></body></html>
"""
   return s

# Receive the Request object
def show(req):
   # The getfirst() method returns the value of the first field with the
   # name passed as the method argument
   word = req.form.getfirst('word', '')
   print "Creating a text file with the write() method."
   text_file = open("/var/www/cgi-bin/input.txt", "w")
   text_file.write(word)
   text_file.close()
   # Escape the user input to avoid script injection attacks
   #word = cgi.escape(word)

   '''Input triggers the application to start its process'''

   output_file=open("/var/www/cgi-bin/output.txt", "r").read()
   s = """\
<html><body>
<p>The submitted word was "%s"</p>
<p><a href="./fill">Submit another word!</a></p>
</body></html>
"""
   return s % output_file

This is my program, here i need to send text to my application and output of the application is written on a file called output.txt, But the application takes some time depending upon the size of the input, problem is this statement output_file=open("/var/www/cgi-bin/output.txt", "r").read() is reading output.txt before the application writes new data over the file. I want that output_file should wait till the application finishes execution and should take updated data from output.txt.

I tried a lot but couldn't figure out, Please help me out through this :)

Thank you :)

Bhuvan raj
  • 413
  • 3
  • 8
  • 17

1 Answers1

0

Let's say you have three different requests that come in to your application at the same time. All of them are going to be attempting to write to and read from the same files at the same time.

For your above code to work with concurrent requests, you need to use unique filenames for each request/response. Come up with a strategy to change include a unique identifier (e.g. timestamp + client ip address) within the filenames for your input and output files.

AJ.
  • 27,586
  • 18
  • 84
  • 94
  • Actually i thought i should wait till application has finished its execution, it shouldn't enter to this statement `output_file=open("/var/www/cgi-bin/output.txt", "r").read()` before the application finishes its job Program should continue only after output.txt is updated by application.. – Bhuvan raj May 14 '11 at 12:54