• Now all data be ready for publish, We had made our blog alive now. Haha.



  • These days CCP GFW blocked all the IPs from US and when I switch my IP in google Cloud, the disk data cannot be restore again. I have to restart my blog totally, Now the website is still building…



Store large data or a service connection per Flask session- Stack Overflow

Python PingBook 1 months ago (10-23) 12 0

Question

I’m writing a small Flask application and am having it connect to Rserve using pyRserve. I want every session to initiate and then maintain its own Rserve connection.

Something like this:

session[‘my_connection’] = pyRserve.connect()

doesn’t work because the connection object is not JSON serializable. On the other hand, something like this:

flask.g.my_connection = pyRserve.connect()

doesn’t work because it does not persist between requests. To add to the difficulty, it doesn’t seem as though pyRserve provides any identifier for a connection, so I can’t store a connection ID in the session and use that to retrieve the right connection before each request.

Is there a way to accomplish having a unique connection per session?

Answer

The following applies to any global Python data that you don’t want to recreate for each request, not just rserve, and not just data that is unique to each user.

We need some common location to create an rserve connection for each user. The simplest way to do this is to run a multiprocessing.Manager as a separate process.

import atexit
from multiprocessing import Lock
from multiprocessing.managers import BaseManager
import pyRserve

connections = {}
lock = Lock()

def get_connection(user_id):
with lock:
if user_id not in connections:
connections[user_id] = pyRserve.connect()

return connections[user_id]

@atexit.register
def close_connections():
for connection in connections.values():
connection.close()

manager = BaseManager((”, 37844), b’password’)
manager.register(‘get_connection’, get_connection)
server = manager.get_server()
server.serve_forever()

Run it before starting your application, so that the manager will be available:

python rserve_manager.py

We can access this manager from the app during requests using a simple function. This assumes you’ve got a value for “user_id” in the session (which is what Flask-Login would do, for example). This ends up making the rserve connection unique per user, not per session.

from multiprocessing.managers import BaseManager
from flask import g, session

def get_rserve():
if not hasattr(g, ‘rserve’):
manager = BaseManager((”, 37844), b’password’)
manager.register(‘get_connection’)
manager.connect()
g.rserve = manager.get_connection(session[‘user_id’])

return g.rserve

Access it inside a view:

result = get_rserve().eval(‘3 + 5’)

This should get you started, although there’s plenty that can be improved, such as not hard-coding the address and password, and not throwing away the connections to the manager. This was written with Python 3, but should work with Python 2.




Copyright from PingBook Blog, If not specified, they are original. This site uses BY-NC-SAProtocol authenticated.
For reprinting, please indicate the link of the original text:Store large data or a service connection per Flask session- Stack Overflow
LIKE (0)
[1725641479@qq.com]
SHARE (0)
PingBook
Author:
We create, We sharing! Tag every value data your sharing
Submit comments
Cancel comments
emoji picture bold strikethrough center italic check in

Hi,you need to provide your name and email adress!

  • Name (Required)
  • Email (Required)
  • Website