M
M
Mist82015-03-26 01:51:31
Flask
Mist8, 2015-03-26 01:51:31

How to update page content obtained by parsing another page in Flask?

There is some basic Flask code like this:

from flask import Flask
from test import text

app = Flask(__name__)

@app.route('/')
def main_page():
    return text

if __name__ == '__main__':
    app.run()


And there is a module that parses the content of a page on the Internet, for example:
from bs4 import BeautifulSoup
import urllib.request

x = urllib.request.urlopen('http://www.example.com')
soup = BeautifulSoup(x)
text = str(soup.body.p)


I need to make sure that the text variable is updated at some interval (i.e. the page is constantly parsed and the result is available for display on my Flask server).

But what happens to me is that the text variable in the module is parsed once, and the page on my server always displays what was parsed once.

I don't understand how I can make the text variable parsed with a time interval and be available for processing by the Flaska script.

I don't know which way to google.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tark, 2015-03-26
@Mist8

Option one, not very good:

from flask import Flask
import test  # именно так

app = Flask(__name__)

@app.route('/')
def main_page():
    # постоянно актуальная информация, даже если переменная изменится в другом месте
    return test.text

if __name__ == '__main__':
    app.run()

This is bad because it will only work with one worker. That is, if you have uwsgi, say, with two processes, then some requests will fall on the "old" value of test.text. If forced to work with one worker, it will work correctly.
Option two, better: use a cache or a database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question