0

I run thin code.

And I want that after 20 seconds that the variable msg will get the value "hello msg2".

And if I refresh the page I see there "hello msg2" instead of "msg1".

from flask import Flask, render_template
import time

mag = "msg1"
app = Flask(__name__)

@app.route("/")
def home():
     return render_template("home.html")

@app.route("/index.html")
def user():
     return render_template("index.html", msg=msg)

if __name__ == "__main__":
    app.run(port=7654, debug=True)

The index.html

<!doctype html>
<html>
<head>
    <title>Home page</title>
</head>

<body>
    <h1>Home Page!5 {{ msg }} </h1>
</body>
</html>

It is possible? Because I could not run any more commands in Python while the site was running.

yosi
  • 65
  • 6

3 Answers3

1

You can use threading to spawn up a background thread to take care of updating value of msg. Use time.sleep() to add a delay to the execution.

from flask import Flask, render_template
import time
import threading

msg = "msg1"

# utility to change msg variable
def change_msg(delay=20):
    time.sleep(delay)
    global msg
    msg = "msg2"

app = Flask(__name__)

@app.route("/")
def home():
     return render_template("home.html")

@app.route("/index.html")
def user():
     return render_template("index.html", msg=msg)

if __name__ == "__main__":
    # spawn a thread that changes value of msg after a delay
    t = threading.Thread(target=change_msg)
    t.start()
    app.run(port=7654, debug=True)
ashwani
  • 690
  • 3
  • 16
0

The easiest way to do what you asked for is a Timer. In your case the content of some_function() should be in your handler function user().

from threading import Timer
from time import sleep

msg = "msg1"

def setMessage():
    global msg
    msg = "msg2"
    print("Message has changed")

def some_function():
    global msg
    timer = Timer(1.0, setMessage)
    timer.start()
    print(msg)


some_function()
sleep(3)
some_function()

Expected output:

msg1
Message has changed
msg2
Message has changed

Note: setMessage() is called twice here, but you could check if the message is already msg2 before starting the timer to prevent this from happening.

If what you actually want is concurrency or parallelism you should have a look at Python Threading or for asynchronous programming at asyncio.

Mushroomator
  • 6,516
  • 1
  • 10
  • 27
  • i can do this with a flask server? – yosi Mar 19 '22 at 19:46
  • Yes, the only thing you actually need is the [Timer](https://docs.python.org/3/library/threading.html#timer-objects), which let's you execute code asynchronously so the timer is startet but the program just goes on to the next line without waiting for the timer to *complete its work*. Only after the specified time the timer will then call the function you have provided and the code there will be run. I suggest you read the introduction to Threading that I have linked in my post to understand what is going on. – Mushroomator Mar 19 '22 at 19:50
  • i read about this. tanks – yosi Mar 19 '22 at 19:52
0

You'll need some Javascript. The process you're looking at is

  1. Load the page which makes a call to your server (python)

  2. Check for the presence of a cookie which tells you the page has been loaded before. If this cookie is not present, you return "msg1" and set a cookie letting you know you've returned "msg1". If the cookie is present, then you return "hello msg2" and you don't have to set the cookie again.

  3. This part is where Javascript comes in - After 20 seconds, make another call to your server. You can do this asynchronously so that your page is not reloaded. The cookie will be sent along and step 2 above comes into play.

NoCommandLine
  • 5,044
  • 2
  • 4
  • 15
  • i want to do somting in python and change the data after i do this. like input or read data from updateing csv etc – yosi Mar 19 '22 at 20:00