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)

   test(0)

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

   simplified_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 % simplified_file


def test(flag):
    print flag
    while flag!=1:
        x=1
    return

This mod_python program's fill method send the text to show method where it writes to input.txt file which is used by my application, till my application is running i don't want rest of the statements to work so i have called a function test, in which i have a while loop which will be looping continuously till the flag is set to 1. If its set to 1 then it will break the while loop and continue execution of rest of the statements. I have made my application to pass test flag variable to set it as 1. according to my logic, it should break the loop and return to show function and continue executing rest but its not happening in that way, its continuously loading the page!

please help me through this..

Thank you.. :)

1
  • You seriously need to think about the logic again. As the answerers have pointed out, the test function will simply run forever and not return. It doesn't matter where you call it from, if you don't call it with 1 it will never end. Commented May 14, 2011 at 21:16

2 Answers 2

2
    while flag!=1:
        x=1

This loop won't ever finish. When is flag ever going to change so that flag != 1 is False? Remember, flag is a local variable so changing it anywhere else isn't going to have an effect -- especially since no other code is going to have the opportunity to run while that loop is still running.

It's really not very clear what you're trying to achieve here. You shouldn't be trying to delay code with infinite loops. I'd re-think your architecture carefully.

Sign up to request clarification or add additional context in comments.

3 Comments

its actually my application which is written in python will finish its execution after all its work it will call this function test and sends parameter 1 so as it will make the while loop to break and it should return to show method and continue executing rest of the code.
I have imported this file in my application and called like this my_program.test(1) so that flag that i have used will only be affected.
If I understand correctly, you're fundamentally misunderstanding how functions work. Calling test(0) will cause an infinite loop, you can't just call test(1) subsequently to break the loop. Moreover, it's very bad practice to use infinite loops to delay portions of code.
0

it is not the most elegant way to do this, but if you need to change the value of flag outside your method, you should use it as a global variable.

def test():
    global flag        # use this everywhere you're using flag.
    print flag
    while flag!=1:
        x=1
    return

but to make a waiting method, have a look to python Event() objects, they have a wait() method that blocks until the Event's flag is set.

1 Comment

i have to use flag locally i don't need any other methods watching it, it has nothing to do with other methods either, i have called this method in my application like this my_program.test(1) which sets flag value to 1, I actually want the show method's read statement to wait till application finish its work, so have done like this.. I recon there's some problem to do with return, its actually sending back the control to my application, how to make it to send the control to show method?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.