Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pylons REPL reevaluate code in running web server

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?

like image 292
John Lawrence Aspden Avatar asked Nov 14 '22 01:11

John Lawrence Aspden


1 Answers

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!

like image 195
John Lawrence Aspden Avatar answered Dec 20 '22 03:12

John Lawrence Aspden