Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

python - How to share session values created outside the context in Flask?

How do I store something created by a thread, in a session, so I can access that value later in another request?

Here is a sample:

@app.route('/one')
def one():
    @copy_current_request_context
    def x():
        session['status'] = "done"      

    t = threading.Thread(target=x)
    t.start()
    return "One"



@app.route('/two')
def two():
    status = session['status']
    return "Two: {}".format(status)

In example above I store the 'status' from within the thread (I need to run the thread) inside the /one request but later, let's say 5s, I want to check for the status in another request (/two).

Also does @copy_current_request_context make a read-only (or read and discard write) copy of the session/request?

question from:https://stackoverflow.com/questions/66064183/how-to-share-session-values-created-outside-the-context-in-flask

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The easiest and somehow best answer is using global variables that have been described completely Here.

But if your application is going to be scaled and you need this data to be shared with other instances, you might use "Redis" as a fast In-Memory DB. More details


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...