How can a unique Rserve connection be stored per session?
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?
python r flask rserve pyrserve
|
show 1 more comment
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?
python r flask rserve pyrserve
Why do you need to use the same connection for a session?
– dirn
Feb 10 '15 at 2:40
Because I need objects in the R namespace to persist for the same user during a session (but not be visible / accessible to other users). For instance, a user may load some data and fit a model - I want to be able to access that model (without refitting it) on other pages (ie. after other Flask requests have been made).
– izyda
Feb 10 '15 at 5:39
I see. I'm not certain I truly need a re-usable connection per user. My only requirement is that a user's R connection/session be able to access R objects created using previous requests by that user. I suppose a workable solution might be to have an R connection save the current R workspace to the server, save the ID of that workspace as a cookie, and upon a new request, have a new R connections read that workspace back...
– izyda
Feb 10 '15 at 6:35
Take a look at DeployR (deployr.revolutionanalytics.com) - it adds APIs and additional functionality on top of Rserve that makes it easy to manage this type of requirement.
– Andrie
Feb 10 '15 at 8:24
@Andrie I considered this - although looks like there are only client libraries for Java, Javascript, and .NET. I'm restricted to python...
– izyda
Feb 10 '15 at 17:52
|
show 1 more comment
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?
python r flask rserve pyrserve
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?
python r flask rserve pyrserve
python r flask rserve pyrserve
edited Feb 10 '15 at 8:16
davidism
64.9k12172188
64.9k12172188
asked Feb 10 '15 at 2:27
izydaizyda
405215
405215
Why do you need to use the same connection for a session?
– dirn
Feb 10 '15 at 2:40
Because I need objects in the R namespace to persist for the same user during a session (but not be visible / accessible to other users). For instance, a user may load some data and fit a model - I want to be able to access that model (without refitting it) on other pages (ie. after other Flask requests have been made).
– izyda
Feb 10 '15 at 5:39
I see. I'm not certain I truly need a re-usable connection per user. My only requirement is that a user's R connection/session be able to access R objects created using previous requests by that user. I suppose a workable solution might be to have an R connection save the current R workspace to the server, save the ID of that workspace as a cookie, and upon a new request, have a new R connections read that workspace back...
– izyda
Feb 10 '15 at 6:35
Take a look at DeployR (deployr.revolutionanalytics.com) - it adds APIs and additional functionality on top of Rserve that makes it easy to manage this type of requirement.
– Andrie
Feb 10 '15 at 8:24
@Andrie I considered this - although looks like there are only client libraries for Java, Javascript, and .NET. I'm restricted to python...
– izyda
Feb 10 '15 at 17:52
|
show 1 more comment
Why do you need to use the same connection for a session?
– dirn
Feb 10 '15 at 2:40
Because I need objects in the R namespace to persist for the same user during a session (but not be visible / accessible to other users). For instance, a user may load some data and fit a model - I want to be able to access that model (without refitting it) on other pages (ie. after other Flask requests have been made).
– izyda
Feb 10 '15 at 5:39
I see. I'm not certain I truly need a re-usable connection per user. My only requirement is that a user's R connection/session be able to access R objects created using previous requests by that user. I suppose a workable solution might be to have an R connection save the current R workspace to the server, save the ID of that workspace as a cookie, and upon a new request, have a new R connections read that workspace back...
– izyda
Feb 10 '15 at 6:35
Take a look at DeployR (deployr.revolutionanalytics.com) - it adds APIs and additional functionality on top of Rserve that makes it easy to manage this type of requirement.
– Andrie
Feb 10 '15 at 8:24
@Andrie I considered this - although looks like there are only client libraries for Java, Javascript, and .NET. I'm restricted to python...
– izyda
Feb 10 '15 at 17:52
Why do you need to use the same connection for a session?
– dirn
Feb 10 '15 at 2:40
Why do you need to use the same connection for a session?
– dirn
Feb 10 '15 at 2:40
Because I need objects in the R namespace to persist for the same user during a session (but not be visible / accessible to other users). For instance, a user may load some data and fit a model - I want to be able to access that model (without refitting it) on other pages (ie. after other Flask requests have been made).
– izyda
Feb 10 '15 at 5:39
Because I need objects in the R namespace to persist for the same user during a session (but not be visible / accessible to other users). For instance, a user may load some data and fit a model - I want to be able to access that model (without refitting it) on other pages (ie. after other Flask requests have been made).
– izyda
Feb 10 '15 at 5:39
I see. I'm not certain I truly need a re-usable connection per user. My only requirement is that a user's R connection/session be able to access R objects created using previous requests by that user. I suppose a workable solution might be to have an R connection save the current R workspace to the server, save the ID of that workspace as a cookie, and upon a new request, have a new R connections read that workspace back...
– izyda
Feb 10 '15 at 6:35
I see. I'm not certain I truly need a re-usable connection per user. My only requirement is that a user's R connection/session be able to access R objects created using previous requests by that user. I suppose a workable solution might be to have an R connection save the current R workspace to the server, save the ID of that workspace as a cookie, and upon a new request, have a new R connections read that workspace back...
– izyda
Feb 10 '15 at 6:35
Take a look at DeployR (deployr.revolutionanalytics.com) - it adds APIs and additional functionality on top of Rserve that makes it easy to manage this type of requirement.
– Andrie
Feb 10 '15 at 8:24
Take a look at DeployR (deployr.revolutionanalytics.com) - it adds APIs and additional functionality on top of Rserve that makes it easy to manage this type of requirement.
– Andrie
Feb 10 '15 at 8:24
@Andrie I considered this - although looks like there are only client libraries for Java, Javascript, and .NET. I'm restricted to python...
– izyda
Feb 10 '15 at 17:52
@Andrie I considered this - although looks like there are only client libraries for Java, Javascript, and .NET. I'm restricted to python...
– izyda
Feb 10 '15 at 17:52
|
show 1 more comment
1 Answer
1
active
oldest
votes
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28423069%2fhow-can-a-unique-rserve-connection-be-stored-per-session%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
edited Apr 17 '18 at 17:56
answered Feb 10 '15 at 8:07
davidismdavidism
64.9k12172188
64.9k12172188
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28423069%2fhow-can-a-unique-rserve-connection-be-stored-per-session%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why do you need to use the same connection for a session?
– dirn
Feb 10 '15 at 2:40
Because I need objects in the R namespace to persist for the same user during a session (but not be visible / accessible to other users). For instance, a user may load some data and fit a model - I want to be able to access that model (without refitting it) on other pages (ie. after other Flask requests have been made).
– izyda
Feb 10 '15 at 5:39
I see. I'm not certain I truly need a re-usable connection per user. My only requirement is that a user's R connection/session be able to access R objects created using previous requests by that user. I suppose a workable solution might be to have an R connection save the current R workspace to the server, save the ID of that workspace as a cookie, and upon a new request, have a new R connections read that workspace back...
– izyda
Feb 10 '15 at 6:35
Take a look at DeployR (deployr.revolutionanalytics.com) - it adds APIs and additional functionality on top of Rserve that makes it easy to manage this type of requirement.
– Andrie
Feb 10 '15 at 8:24
@Andrie I considered this - although looks like there are only client libraries for Java, Javascript, and .NET. I'm restricted to python...
– izyda
Feb 10 '15 at 17:52