Pylons REPL 重新评估正在运行的 Web 服务器中的代码

发布于 2024-10-26 11:44:57 字数 1303 浏览 2 评论 0原文

我正在一个预先存在的 pylons 项目(okfn 的 ckan)上用 python 进行编程,但我是一个 lisper,并且习惯了这种做事方式。

如果我做了错误的陈述,请纠正我:

在 pylons 中,似乎我应该说

$ Pasterserve --reload

来获取会注意到更改的 Web 服务器。

那时我可以更改函数,保存文件,然后转到浏览器来测试更改。

如果我想在制作网页的过程中检查函数中的变量,那么我会提出“hello”,然后当我加载页面时,我会得到一个基于浏览器的调试器,我可以在其中检查程序。

这一切都非常好并且工作顺利,我的印象是人们倾向于这样编写 pylons 代码。

不幸的是,重新加载需要几秒钟,它不断打断我的思路。

我想做的是从 emacs 运行 Web 服务器(尽管命令行上的 python REPL 几乎一样好),这样我就可以在编辑器中更改函数,然后将新代码发送到运行进程而无需重新启动它。 (使用命令行 repl 我想我必须复制并粘贴新的东西,但这也是可行的,只是稍微不太方便)

Python 看起来非常动态,并且在很多方面都很像 lisp,所以我不能原则上看看这行不通的任何原因。

所以我想问题是:

有人熟悉 lisp 的做事方式和 Pylons,他们能告诉我如何在 pylons 中以 lisp 方式编程吗?或者由于某种原因这是不可能的或者是一个坏主意?

编辑:

我可以从 emacs 内的 python 解释器运行网络服务器:

from paste.script.serve import ServeCommand
ServeCommand("serve").run(["development.ini"])

我可以让代码停止并通过插入显示它在做什么:

import pdb
pdb.set_trace()

所以现在我需要的是一种让网络服务器在不同线程上运行的方法,这样控制权就会返回到 REPL,我可以在运行过程中重新定义函数和变量。

def start_server():
    from paste.script.serve import ServeCommand
    ServeCommand("serve").run(["development.ini"])


server_thread=threading.Thread(target=start_server)
server_thread.start()

这似乎有效,但如果我在 REPL 上重新定义函数,则更改不会反映在网络服务器中。有谁知道为什么?

I'm programming in python on a pre-existing pylons project (the okfn's ckan), but I'm a lisper by trade and used to that way of doing things.

Please correct me if I make false statements:

In pylons it seems that I should say

$ paster serve --reload

to get a web server that will notice changes.

At that point I can change a function, save the file and then go to my browser to test the change.

If I want to examine variables in a function in the process of making a webpage, then I put raise "hello", and then when I load the page, I get a browser based debugger, in which I can examine the program.

This is all very nice and works swimmingly, and I get the impression that that's how people tend to write pylons code.

Unfortunately the reload takes several seconds, and it keeps breaking my train of thought.

What I'd like to do is to run the web server from emacs, (although a python REPL on the command line would be almost as good), so that I can change a function in the editor and then send the new code to the running process without having to restart it. (with a command line repl I guess I'd have to copy and paste the new thing, but that would also be workable, just slightly less convenient)

Python seems very dynamic, and much like lisp in many ways, so I can't see in principle any reason why that wouldn't work.

So I guess the question is:

Is anyone familiar with the lisp way of doing things, and with Pylons, and can they tell me how to program the lisp way in pylons? Or is it impossible or a bad idea for some reason?

Edit:

I can run the webserver from my python interpreter inside emacs with:

from paste.script.serve import ServeCommand
ServeCommand("serve").run(["development.ini"])

And I can get the code to stop and show me what it's doing by inserting:

import pdb
pdb.set_trace()

so now all I need is a way to get the webserver to run on a different thread, so that control returns to the REPL and I can redefine functions and variables in the running process.

def start_server():
    from paste.script.serve import ServeCommand
    ServeCommand("serve").run(["development.ini"])


server_thread=threading.Thread(target=start_server)
server_thread.start()

This seems to work, except that if I redefine a function at the REPL the change doesn't get reflected in the webserver. Does anyone know why?

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

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

发布评论

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

评论(1

浅忆 2024-11-02 11:44:57

由于 TokenMacGuy 的评论给出的原因,这种工作方式在 python 中似乎是不可能的,即因为重新定义类不会更改该类实例中的代码。

这似乎是一个可怕的耻辱,因为在许多其他方面 python 看起来非常灵活,但它确实解释了为什么没有 python-swank!

It seems that this way of working is impossible in python for the reason given by TokenMacGuy's comment, i.e. because redefining a class doesn't change the code in an instance of that class.

That seems a terrible shame, since in many other respects python seems very flexible, but it does explain why there's no python-swank!

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