简体   繁体   English

如何使mod_python等待更新的文本文件读取?

[英]How to make mod_python to wait for the updated text file to read?

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. 这是我的程序,在这里我需要将文本发送到我的应用程序,并且该应用程序的输出写在一个名为output.txt的文件上,但是该应用程序需要一些时间,具体取决于输入的大小,问题是此语句output_file=open("/var/www/cgi-bin/output.txt", "r").read()在应用程序在文件上写入新数据之前正在读取output.txt。 I want that output_file should wait till the application finishes execution and should take updated data from output.txt. 我希望output_file可以等到应用程序完成执行,并应该从output.txt中获取更新的数据。

I tried a lot but couldn't figure out, Please help me out through this :) 我尝试了很多,但无法弄清楚,请通过以下方法帮助我:)

Thank you :) 谢谢 :)

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 (eg timestamp + client ip address) within the filenames for your input and output files. 提出一种更改策略,在输入和输出文件的文件名中包含唯一标识符(例如,时间戳+客户端IP地址)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM