如何设置mod_python配置变量?
我正在使用 mod_python 运行 Python 服务器,并且遇到了一些配置变量问题。 这实际上是两个问题合二为一,因为我认为它们高度相关:
我需要一种方法来配置运行时在 Python 中可用的变量。 我目前只有一个模块,它设置一些导入到其他模块中的名称-值对,但我正在阅读 PythonOption 最近,我想知道使用它会带来什么优势。
我需要一种在服务器上存储状态的方法。 我可以访问一个每天只能运行 X 次的 API,一旦达到该限制,我需要恢复到我的(较少的)代码。 我想知道如何跟踪一天内运行查询的次数。
我考虑过使用文件或数据库,但我担心让每个人都尝试同时访问相同的文件或行会减慢请求速度。 有没有更好的方法在 mod_python 中进行设置?
I'm running a Python server with mod_python, and I've run into some issues with configuration variables. This is actually two questions rolled into one, because I think they are highly related:
I need a way to configure variables that will be available in Python while running. I currently just have a module that sets some name-value pairs that I import into other modules, but I was reading up on PythonOption recently and was wondering what advantages would be gained from using that instead.
I need a way to store state on the server. I've got access to an API that's limited to running X number of times a day, and once it hits that limit, I need to revert to my (lesser) code. I'm wondering how I can keep track of how many times I've run the query in a day.
I thought about using a file or the database, but I'm afraid I will slow down requests by having everyone try to access the same file or row at once. Is there a better way to set this up in mod_python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
PythonOption
可以让您配置可能需要在服务器之间更改的内容。 不过,我不会太多地使用它,因为弄乱 Apache 配置指令是一种痛苦(而且它需要重新加载服务器)。 您可能会考虑使用 PythonOption 来指定包含实际配置变量的设置文件的名称。 (或者您可以像大多数框架一样,在标准位置中查找设置文件)如果您确实不想考虑文件或数据库,请尝试
memcached
。 它基本上是一个非常简单的数据库(仅通过按键获取、设置和清除),完全存储在 RAM 中,因此速度非常快。 不过,如果您需要存储的只是一个计数器变量,您可能可以将其作为全局变量粘贴在 Python 模块中,除非您担心重新加载模块时计数器会被重置。Using
PythonOption
lets you configure stuff that may need to change from server to server. I wouldn't use it too much, though, because messing with the Apache configuration directives is kind of a pain (plus it requires reloading the server). You might consider something like usingPythonOption
to specify the name of a settings file that contains the actual configuration variables. (Or you could just look in a standard location for the settings file, like most frameworks do)If you really don't want to consider a file or a database, try
memcached
. It's basically a very simple database (get, set, and clear by key only) that's stored entirely in RAM, so it's very fast. If all you need to store is a counter variable, though, you could probably just stick it in a Python module as a global variable, unless you're worried about the counter being reset when the module gets reloaded.我需要一种方法来配置运行时在 Python 中可用的变量。
做 Django 所做的事情。 使用 Python 赋值语句的简单可导入脚本。
我需要一种在服务器上存储状态的方法。
做 Django 所做的事情。 使用 SQLite3。
另外,请阅读 PEP 333 并构建您的应用程序组件以支持 WSGI。 这应该是一个相对较小的修订,以使现有代码具有正确的 WSGI 结构。
当您从 mod_python 切换到 mod_wsgi 时,您可以找到许多组件来为您完成这些操作并减少您编写的代码量。
I need a way to configure variables that will be available in Python while running.
Do what Django does. Use a simple importable script of Python assignment statements.
I need a way to store state on the server.
Do what Django does. Use SQLite3.
Also, read PEP 333 and structure your application components to support WSGI. This should be a relatively small revision to have the proper WSGI structure to existing code.
When you switch from mod_python to mod_wsgi, you can find numerous components to do these things for you and reduce the amount of code you've written.