如何让mod嫒python等待更新的文本文件读取?

2024-03-29 15:03:38 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是我的程序,这里我需要向我的应用程序发送文本,而应用程序的输出则写在一个名为输出.txt,但根据输入的大小,应用程序需要一些时间,问题是output_file=open("/var/www/cgi-bin/output.txt", "r").read()正在读取此语句输出.txt在应用程序将新数据写入文件之前。我希望输出_文件应该等到应用程序完成执行并从中获取更新的数据输出.txt. 在

我试了很多次,但还是想不出来,请帮我解决这个问题:)

谢谢:)


Tags: thetextformtxt应用程序inputoutputvar
1条回答
网友
1楼 · 发布于 2024-03-29 15:03:38

假设您有三个不同的请求同时进入您的应用程序。所有这些文件都将试图同时对相同的文件进行读写操作。在

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

相关问题 更多 >