pallets-eco / flask-session

Server side session extension for Flask
https://flask-session.readthedocs.io
BSD 3-Clause "New" or "Revised" License
501 stars 239 forks source link

New session for every new connection #84

Closed ehteshamshareef closed 7 months ago

ehteshamshareef commented 6 years ago

I'm using Flask-Session and redis for an application im trying to make and im using gunicorn with 4 gevent workers as the production server. I'm trying to build a counter which should have a seperate session id each time a new browser or tab is opened and each session id is supposed to maintain its own count.

Here's the code that i have till now.

from flask import Flask
import os
from flask.json import jsonify
from multiprocessing import Value
from flask import Flask, session
from flask_session import Session
import redis  
import os

def create_app():
        app = Flask(__name__)
    app.secret_key = '123456789012345678901234'
    app.config['SESSION_TYPE'] = 'redis'
    app.config['SESSION_REDIS'] = redis.from_url('localhost:6379')
    # app.config.from_object(__name__)

    sess = Session()
    sess.init_app(app)

    @app.route('/set/')
    def set():
         session['key'] = 1
         return 'ok'

    @app.route('/get/')
    def get():
        session['key'] = session.get('key','not set') + 1
        return 'Count: '+ str(session.get('key','not set')) + ', Process ID: ' +str(os.getpid())

    return app

In the 'set' route, im setting the key value to 1 and whenever i go to 'get' the counter value increases.

I'm trying to figure out how to set session id for each new connection( new tab or browser) and then each session id holds it's count. Any help would be appreciated.

Lxstr commented 7 months ago

In my testing of the latest version this works perfectly. I would note that you must use entirely different browsers or clear the cookie as most browsers will use the same cookie jar for all of their tabs and windows.