Is it possible to change a script when it is running?

Have Users Change a Running Python Script's Variables over the Web?

  • I'm writing a simple simulator with python and I would like to be able to have my users log in to the simulator over the internet and change their own set of variables. I know next-to-nothing on web programming and I'm not to concerned about security, the simulation will be on it's own dedicated machine.

  • Answer:

    You need to be a lot more specific. I would like to be able to have my users: log on to the simulator over the internet. What do you mean by "log on?" Are they going to be given a username and a password? Is each user going to have their own username and password? change their own set of variables Does this mean that each user is going to have their own instance of the simulation, so that changing their variables only applies to their instance of the simulation? Or is there one instance of the simulation and each user changes a subset of the inputs? Or something else? I know next-to-nothing on web programming and I'm not to concerned about security, the simulation will be on it's own dedicated machine. If you hook your simulation to the web, you are doing web programming. If you are doing web programming, you should be concerned about security. Running the simulation on it's own dedicated machine may be part of a security strategy, but it probably isn't sufficient. You'll probably, at the very least, want to sanitize user input to prevent code injection. You'll also want the simulation running as a user with minimal privileges to make other changes to the machine, and you'll probably want the machine firewalled-off from other systems. Since you are doing web programming, you'll may also need a concurrency strategy for the eventually that two or more users submit inputs at the same time. It is something that they could tweak the parameters while it is running. Is the simulation running continuously, or does it just run in response to user inputs? Is the state of the simulation preserved between inputs, or does it start from the same initial state each time a user tweaks a parameter? I don't need live web updates or anything like that. The results would stay on the machine running the simulation. All I would need is for my users to be able to change the parameters. This doesn't sound like "tweaking" to me. To me, tweaking implies that the user gets to see the results of their changes. Or do you just mean that the user doesn't need changes from the ongoing execution of the simulation pushed to their browser automatically? At a minimum, you'll need an HTTP server. That HTTP server will need to serve a static web page with a form with fields for the inputs users are allowed to make. That form will have to submit those inputs back to the web server. There will need to be a CGI script or some other request handler that parses the request parameters passed along by the web server and executes your simulation with the provided parameters and then provides some sort of HTML response back to the web server to return to the client. I think the python standard library has a CGI module to help with this sort of thing. The CGI approach will be fine for situations when the simulation executes for a relatively short period of time after receiving new inputs. It will work less well if the simulation is running continuously. In that case, you will need a long-running process for your simulation, and then a way to communicate between your simulation and an HTTP server. One way of doing that is to build a request handler directly into you your simulation, or, build your simulation directly into a request handler. You'd probably want to start with a minimal python web framework. Django sounds like overkill...maybe something like Flask, or Bottle. Follow their recommended practices for deployment. How does your simulation run now? How do people interact with it if it is running locally?

millerizer at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

SSH?

oceanjesse

What's the output of the simulator? Is this just something users set the initial parameters on and it executes, or is this something where users can tweak the parameters as the simulation runs?

RonButNotStupid

Oh! I should have said that. It is something that they could tweak the parameters while it is running.

millerizer

In that case, you really need to collaborate with someone who knows how to do this kind of thing (live web updates, visualization, etc, etc). I don't there's any easy answer.

RonButNotStupid

Okay thanks, but to clarify, I don't need live web updates or anything like that. The results would stay on the machine running the simulation. All I would need is for my users to be able to change the parameters.

millerizer

I think http://pythonhosted.org/Pyro4/ or http://rpyc.readthedocs.org/en/latest/ would do what you want with the least pain. RPyC sounds a little easier to use, although I haven't used either personally. You would build your Web stuff in a Python framework (say Bottle, if you want something tiny and simple, or Django, if you want something fancy and powerful) and use RPyC directly from there to make changes on your remote machine.

pocams

I've often thought about this, have a running program update its code during runtime. In ruby it would be fairly straightforward, I *think* the same approach would work for Python using reload(module). In essence you have a web interface that updates the source code file of the module. In the main program, when the source code is updated you fire the reload command. The running program should then start using the new variables. Note that it's quite easy to crash the program this way. If stability of the running program is important, then pocams RPyC recommendation can be used to better encapsulate what the user is allowed to modify for better code integrity. I always thought it'd be interesting to have a group of coders modify a running program in real time using dynamic code. For example, starting with a basic game, have the game play changed during play. Sounds like a fun experiment to me.

forforf

Assuming that you don't have that many users and that disk-space is no real issue, you could use small text-files to safe the users' inputs and to read from them to change the parameters in your script. All you would need is a loop that checks a directory for an updated file since the last run. It might be good to use timestamps in the filenames for 'historical' reasons. Of course you would need a simple way to safe the data from a webform to those files, but that should be trivial. BUT: this is very basic, definitely not secure, and surely not even a good idea, more like a bad hack. But it could work and gets the job done.

KMB

Here's the simplest way I can think of: Write a PHP page that accepts the parameters, and presumably a fixed password. This doesn't have to be any more complicated than a simple Web form that updates values in a database. For a database you can just use something simple like http://php.net/manual/en/ref.sqlite.php (if the webserver and simulator are the same machine), or MySQL if that's what your webhosting provider has. Then, in your simulation, check the database at intervals to read the current values. Bam. If your simulation is running on a different machine than the webserver, you can simply write another PHP page that exports the settings in JSON or XML, and have the simulation query that page for updates. You can do this using Flask or similar if you'd prefer to use Python, but PHP is much easier to deploy.

neckro23

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.