2011-05-14 29 views
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 

这是我的计划,我在这里需要文本发送到我的应用程序和应用程序的输出被写在名为output.txt的文件,但是,应用程序需要一定的时间,这取决于输入的大小,问题是在应用程序在文件上写入新数据之前,此语句output_file=open("/var/www/cgi-bin/output.txt", "r").read()正在读取output.txt。我希望output_file应该等到应用程序完成执行,并且应该从output.txt中获取更新的数据。如何让mod_python等待更新的文本文件读取?

我尝试了很多,但无法弄清楚,请帮我通过这个:)

谢谢:)

回答

0

比方说,你有在进来您的应用程序三个不同的请求同时。他们都将试图在上同时写入和读取相同的文件

对于上面的代码来处理并发请求,您需要为每个请求/响应使用唯一的文件名。提出一个改变策略,在输入和输出文件的文件名中包含一个唯一的标识符(例如时间戳+客户端IP地址)。

+0

其实我以为我应该等到应用程序执行完毕后才能进入这个语句'output_file = open(“/ var/www/cgi-bin/output.txt”,“r”)。 ()'应用程序完成其作业之前 只有在应用程序更新output.txt后,程序才应该继续。 – 2011-05-14 12:54:26