如何在服务器运行时通过 diff 请求访问只读数据(apache、mod_python)

发布于 2024-08-15 17:11:46 字数 926 浏览 5 评论 0原文

我正在使用 Apache/2.2.8 (Ubuntu) mod_python/3.3.1 Python/2.5.2,我想预加载我使用的数据。

目前,每次收到请求时,我都会从磁盘上的文件中读取数据,然后解析它并将其存储在对象中。数据文件相对较大,我想提前解析/预加载它。

我想我可以1)在apache启动时将数据加载到内存中(服务器运行时大约100MB到500MB的数据将驻留在内存中)或者我可以2)在提交第一个数据请求时加载它并保留它在内存中,直到我关闭服务器。

下面是第二个想法的模型:

from mod_python import apache
from mod_python import Session

gvar = 0

def handler(req):
    req.content_type = 'text/plain'

    session = Session.Session(req)
    if session.is_new():
        global gvar
        req.write('gvar was originally : '+str(gvar))
        gvar = 314
        session['addr'] = req.connection.remote_ip
        session.save()
        req.write('\ngvar was just set to: '+str(gvar))
    else:
        global gvar
        req.write('gvar set to: '+str(gvar))

    return apache.OK  

输出(会话一):
gvar 最初是:0
gvar 刚刚设置为:314

输出(会话 > 1):
gvar 设置为:314

请分享您的意见和解决方案, 谢谢

I am using Apache/2.2.8 (Ubuntu) mod_python/3.3.1 Python/2.5.2 and I would like to preload the data I work with.

Currently I read the data from a file on disk every time I get a request, then parse it and store it in an object. The data file is relatively large and I would like to parse/preload it ahead of time.

I was thinking I could either 1) load the data in memory when apache starts (~100MB to 500MB of data would reside in memory while the server is running) or I could 2) load it when the first data request is submitted and keep it in memory until I shut the server down.

below is the mock up of the second idea:

from mod_python import apache
from mod_python import Session

gvar = 0

def handler(req):
    req.content_type = 'text/plain'

    session = Session.Session(req)
    if session.is_new():
        global gvar
        req.write('gvar was originally : '+str(gvar))
        gvar = 314
        session['addr'] = req.connection.remote_ip
        session.save()
        req.write('\ngvar was just set to: '+str(gvar))
    else:
        global gvar
        req.write('gvar set to: '+str(gvar))

    return apache.OK  

output (session one):
gvar was originally : 0
gvar was just set to: 314

output (session > 1):
gvar set to: 314

Please share your comments and solutions,
thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林迷了鹿 2024-08-22 17:11:46

您可以使用数据设置 tmpfs(或 ramfs)挂载,它将保留在 RAM 中(tmpfs 可能会将数据发送到交换区)。

You could set a tmpfs (or ramfs) mount with the data and it will stay in RAM (tmpfs may send data to swap).

会发光的星星闪亮亮i 2024-08-22 17:11:46

您没有说明数据的形式,但如果密钥库就足够了,那么您可以使用搁置和操作系统缓存来以预先解析的格式保存数据。

You don't say what form your data is in, but if a keystore will suffice then you can use shelve along with OS caching in order to hold the data in a preparsed format.

心在旅行 2024-08-22 17:11:46

另一种选择是使用 posix_ipc 将数据保存在共享内存中,可供所有进程使用。

Another option is to use posix_ipc to hold the data in shared memory, available to all processes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文