本文介绍了每个会话如何存储独特的Rserve连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个小的Flask应用程序,并使用pyRserve连接到Rserve。我希望每个会话都能启动并维护自己的Rserve连接。



类似于这样:

  session ['my_connection'] = pyRserve.connect()

不起作用,因为连接对象不是JSON序列化的。另一方面,类似这样的:

$ $ $ $ $ $ $ $ $ $> flask.g.my_connection = pyRserve.connect()
code>

不起作用,因为它不会在请求之间持续存在。为了增加难度,pyRserve似乎没有为连接提供任何标识符,所以我不能在会话中存储连接ID,并使用它在每个请求之前检索正确的连接。

b
$ b

有没有一种方法可以实现每个会话的独特连接?

解决方案

一些常见的位置为每个用户创建一个rserve连接。最简单的方法是运行作为一个单独的进程。

  import atexit 
from multiprocessing import从multiprocessing.managers中锁定
导入BaseManager
导入pyRserve
$ b连接= {}
lock =锁定()


如果user_id不在连接中:
connections [user_id] = pyRserve.connect()

返回连接[user_id]:
with lock:


$ b @ atexit.register
def connection_connections():
在connections.values()中的连接:
connection.close()


manager = BaseManager(('',37844),b'password')
manager.register('get_connection',get_connection)
server = manager.get_server()
server.serve_forever()

运行它在开始你的应用程序之前,以便经理可以使用:

prey $ rs $ $
$ $ c


$ b $ p
$ b

我们可以在使用简单函数的请求期间从应用程序访问这个管理器。这假设你已经在会话中获得了user_id的值(例如,Flask-Login将会这样做)。这会导致每个用户的rserve连接是唯一的,而不是每个会话。

  from multiprocessing.managers import BaseManager 
from ('',37844),b'',

def get_rserve():
如果不是hasattr(g,'rserve'):
manager = BaseManager密码')
manager.register('get_connection')
manager.connect()
g.rserve = manager.get_connection(session ['user_id'])

返回g.rserve

在视图中访问它:

  result = get_rserve()。eval('3 + 5')






这应该让你开始,虽然有很多可以改进的地方,比如不要硬编码地址和密码,也不要把连接丢掉管理者。这是使用Python 3编写的,但是应该使用Python 2。


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?

解决方案

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.

这篇关于每个会话如何存储独特的Rserve连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 18:43