473,379 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Is it possible to save a running program and reload next time ?

Hi,

I have a program which will continue to run for several days. When it is
running, I can't do anything except waiting because it takes over most
of the CUP time.

Is it possible that the program can save all running data to a file when
I want it to stop, and can reload the data and continue to run from
where it stops when the computer is free ?

Regards,

xiaojf

Sep 21 '06 #1
14 3409

fd********@gmail.com wrote:
Hi,

I have a program which will continue to run for several days. When it is
running, I can't do anything except waiting because it takes over most
of the CUP time.

Is it possible that the program can save all running data to a file when
I want it to stop, and can reload the data and continue to run from
where it stops when the computer is free ?

Regards,

xiaojf
Can't you just use time.sleep to put your program to, uh, sleep? For
example, time.sleep(10) will effectively pause your program for ten
seconds, making it use very few CPU cycles. I don't know what
interface you have, but assuming you can pick up a button or a key
press, you can ask for a length of time, or just put your code into a
holding pattern:

def pause():
paused = True
while paused:
time.sleep(1)
if wake_up_key_pressed:
paused = False

or with a gui:

paused = False

def pause():
global paused
paused = True
while paused:
time.sleep(1)

def onWakeUpButton(): #bind this to button
global paused
paused = False
Iain

Sep 21 '06 #2
fd********@gmail.com wrote:
Is it possible that the program can save all running data to a file when
I want it to stop, and can reload the data and continue to run from
where it stops when the computer is free ?
This isn't what you asked for (I have no idea how to do that), but
given your description, perhaps a different solution would work.

If you're using a *nix type OS (or possibly Cygwin, never tried) with a
bash-style shell, then you probably already have job control built in.
For example, in bash you can type <CTRL>Z to stop the current process,
and you can see all jobs and their states and job numbers with the
'job' command, and you can resume a stopped job with '%[job_number]'.
So for example, if only one job is in he queue, just stop it and then
when you're ready do %1.

Another *nix option would be to use the nice command to set the
schedular priority of the process so that when something with a higher
priority needs the CPU then it gets it first rather than your nice'd
process (something like 'nice -n 15 python your_program.py' should do
well -- very low priority). I'm pretty sure windows has some kind of
priority option for tasks as well, in fact, IIRC you open the task
manager and right click on a task and can set the priority lower.

Regards,
Jordan

Sep 21 '06 #3
fd********@gmail.com wrote:
I have a program which will continue to run for several days. When it is
running, I can't do anything except waiting because it takes over most
of the CUP time.

Is it possible that the program can save all running data to a file when
I want it to stop, and can reload the data and continue to run from
where it stops when the computer is free ?
For Linux (and other Unix like OSs), there are several "checkpointing"
libraries available which allow programs to be saved to disk, and restarted
later.

If the other suggestions people have given to you (STOPping and CONTinuing
processes), or sleep statements, or saving state, don't work investigate
these.

e.g. http://www.cs.wisc.edu/~zandy/ckpt/

These programs have limitations on what can be restored (e.g. threads,
shared memory, network connections...). I don't know which ones work with
python.

--
Jeremy Sanders
http://www.jeremysanders.net/
Sep 21 '06 #4
On 2006-09-21, fd********@gmail.com <fd********@gmail.comwrote:
Is it possible that the program can save all running data to a file when
I want it to stop, and can reload the data and continue to run from
where it stops when the computer is free ?
Well, on Irix you could use cpr
<http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?coll=0650&db=man&fname=/usr/share/catman/u_man/cat1/cpr.z>
but i don't know if your OS supports a similar feature

--
Roland Csaszar ----------- \\\ /// -------------- +43 316 495 2129
Software Development ------ \\\ /// ----------- http://www.knapp.com
KNAPP Logistics Automation - \\V// - mailto:ro************@knapp.com
Sep 21 '06 #5
Jeremy Sanders <je*******************@jeremysanders.netwrote:
For Linux (and other Unix like OSs), there are several "checkpointing"
libraries available which allow programs to be saved to disk, and
restarted later.
Another option which will work on just about anything would be to run the
program in a vmware virtual machine. Then you can suspend or resume the
virtual machine whenever you wish.
Sep 21 '06 #6
Dennis Lee Bieber wrote:
On Thu, 21 Sep 2006 15:34:21 +0800, "fd********@gmail.com"
<fd********@gmail.comdeclaimed the following in comp.lang.python:

>Is it possible that the program can save all running data to a file when
I want it to stop, and can reload the data and continue to run from
where it stops when the computer is free ?

You'd have to write the code to save state, which means you'd have
to be at a known resumable point... I suspect that, if you're nested
into a recursive function, that will not be possible as you'd have to
recreate the calling stack along with populating all know data.

It should be pointed out that "chaos theory" sort of got its start
from an attempt at doing something similar -- saving a massive data
array for later reloading. It turns out the saved data didn't have the
full resolution of the computations, so the restart from the saved data
diverged rapidly from where the prior run had been heading. (As I
recall, it was a weather simulation in the late 60s early 70s, and the
data was saved in single-precision text format -- meaning round-off
errors on output/input; these days it would be the equivalent of the
losses inherent in doing a long computation in 80-bit IEEE, but saving
intermediates [for reload] in 64-bit IEEE).

The other option may not be available under most operating system...
Namely a snapshot of the entire "core" of memory, so you can restore the
machine /exactly/ to the state it had been at the time of the snapshot
(this would probably affect everything on the machine -- all other
programs, etc.)
Though if someone else have more information contradicting me, I'd
be happy to hear it... <G>
Thank you all!

Can objects be saved and reloaded by "Pickle" ? I have tried but no
success.

One of my friends tell me that VMware can save the current state of a
virtual machine by suspending it, and can restore the identical state
later. When a virtual machine is suspended , a file with a .vmss
extension is created(about 20M), which contains the entire state of the
virtual machine. When you resume the virtual machine, its state is
restored from the .vmss file.

So can this method be implemented with python ?

I have another idea. When python is running, it uses about 10M memory.
So can I just save a copy of the memory used by python and restore it
later ?

Regards,

xiaojf

Sep 21 '06 #7
fd********@gmail.com wrote:
Hi,

I have a program which will continue to run for several days. When it is
running, I can't do anything except waiting because it takes over most
of the CUP time.

Is it possible that the program can save all running data to a file when
I want it to stop, and can reload the data and continue to run from
where it stops when the computer is free ?

Regards,

xiaojf
You can save the state of Python objects and reload them at a later time/date by
using Zope's ZODB.

http://www.zope.org/Wikis/ZODB/FrontPage

-Larry Bates
Sep 21 '06 #8
fd********@gmail.com wrote:
Hi,

I have a program which will continue to run for several days. When it is
running, I can't do anything except waiting because it takes over most
of the CUP time.

Is it possible that the program can save all running data to a file when
I want it to stop, and can reload the data and continue to run from
where it stops when the computer is free ?

Regards,

xiaojf
You can save the state of Python objects and reload them at a later time/date by
using Zope's ZODB.

http://www.zope.org/Wikis/ZODB/FrontPage

-Larry Bates

Sep 21 '06 #9
fd********@gmail.com wrote:
>
Can objects be saved and reloaded by "Pickle" ? I have tried but no
success.
Yes, that's the intended use of pickle/cPickle. There are examples in
the docs:

http://docs.python.org/lib/module-pickle.html

What have you tried and what didn't work?

Hans Georg
Sep 21 '06 #10
fd********@gmail.com wrote:
I have a program which will continue to run for several days. When it is
running, I can't do anything except waiting because it takes over most
of the CUP time.

Is it possible that the program can save all running data to a file when
I want it to stop, and can reload the data and continue to run from
where it stops when the computer is free ?
Yes. Stackless Python can allow you to pickle running code and
to save it to disk and load it and continue running it from where it
left off. It won't of course work if there is external state because
this will not be there, or necessarily still be relevant when the code
resumes. So as long as you know the limitations and they suit
your uses, it should be usable for this.

http://www.stackless.com

Here's an example:

import stackless
import cPickle
import time

def f():
n = 0
while True:
n += 1
stackless.schedule()

t = stackless.tasklet(f)()

while True:
# Run the tasklet until it yields or dies.
t.run()
# Yield to the operating system.
try:
time.sleep(0.1)
except KeyboardInterrupt:
if not t.alive:
print "Tasklet unexpectedly dead"
break

# Serialise the running tasklet.
v = cPickle.dumps(t)
# Kill the old version.
t.kill()

# y / enter = continue
# anything else = exit
# otherwise stalled
s = raw_input("Continue? [y]:").lower()
if s not in ("y", ""):
break

t = cPickle.loads(v)

Unfortunately KeyboardInterrupt or probably any other
exception as a way of interrupting the running code is
problematic as it will kill the running tasklet if it happens
there instead of during the sleep.

Hope this helps,
Richard.

Sep 21 '06 #11
Hans Georg Krauthaeuser wrote:
fd********@gmail.com wrote:
>Can objects be saved and reloaded by "Pickle" ? I have tried but no
success.

Yes, that's the intended use of pickle/cPickle. There are examples in
the docs:

http://docs.python.org/lib/module-pickle.html

What have you tried and what didn't work?

Hans Georg
My program is a genetic algorithm based program, so I thought I just
need to save a generation object of the population and reload it later
then my program could continue. But after I saved a generation object
from within python using cPickle and reload it from another program, it
said that some class object couldn't be found. Now I realized that a
program cannot be suspended and reloaded by just simply dumping and
loading using cPickle/Pickle.

I will try stackless python later.

Thanks a lot .

Regards,

xiaojf

Sep 22 '06 #12
fd********@gmail.com wrote:
Hans Georg Krauthaeuser wrote:
>fd********@gmail.com wrote:
>>Can objects be saved and reloaded by "Pickle" ? I have tried but no
success.

Yes, that's the intended use of pickle/cPickle. There are examples in
the docs:

http://docs.python.org/lib/module-pickle.html

What have you tried and what didn't work?

Hans Georg
My program is a genetic algorithm based program, so I thought I just
need to save a generation object of the population and reload it later
then my program could continue. But after I saved a generation object
from within python using cPickle and reload it from another program, it
said that some class object couldn't be found. Now I realized that a
program cannot be suspended and reloaded by just simply dumping and
loading using cPickle/Pickle.

I will try stackless python later.

Thanks a lot .

Regards,

xiaojf
I use pickle to periodically save measured data for measurements that
take several days to finish. Actually, I save a class instance with the
data, the measurement methods and additional informations what method
was called and with what parameters. If the measurement crashes due to
some reasons, I recreate the class instance from the pickle file, call
the appropriate method with the saved parameters. Then, the method has
to look what data is already there and will continue to measure the rest.

I have no idea whether that will help you in your situation.

Anyway, I wish you all the best.

Hans Georg
Sep 22 '06 #13
Hans Georg Krauthaeuser wrote:
fd********@gmail.com wrote:
>Hans Georg Krauthaeuser wrote:
>>fd********@gmail.com wrote:
Can objects be saved and reloaded by "Pickle" ? I have tried but no
success.

Yes, that's the intended use of pickle/cPickle. There are examples in
the docs:

http://docs.python.org/lib/module-pickle.html

What have you tried and what didn't work?

Hans Georg

My program is a genetic algorithm based program, so I thought I just
need to save a generation object of the population and reload it later
then my program could continue. But after I saved a generation object
from within python using cPickle and reload it from another program, it
said that some class object couldn't be found. Now I realized that a
program cannot be suspended and reloaded by just simply dumping and
loading using cPickle/Pickle.

I will try stackless python later.

Thanks a lot .

Regards,

xiaojf

I use pickle to periodically save measured data for measurements that
take several days to finish. Actually, I save a class instance with the
data, the measurement methods and additional informations what method
was called and with what parameters. If the measurement crashes due to
some reasons, I recreate the class instance from the pickle file, call
the appropriate method with the saved parameters. Then, the method has
to look what data is already there and will continue to measure the rest.

I have no idea whether that will help you in your situation.

Anyway, I wish you all the best.

Hans Georg
I just realized another serious problem.

Random values are extensively used in my program, so the program may
probably not continue to run from an absolutely identical state where it
stopped.

Can the state of the random value generator be saved ?

Regards,

xiaojf
Sep 22 '06 #14
fd********@gmail.com wrote:
Can the state of the random value generator be saved ?
Yes. You can pickle random._inst or your own random.Random instance.

Peter

Sep 22 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: ukjock | last post by:
I am very new to the whole Visual Basic .net scene, and I am trying to get to grips with it... I thought I would try and learn a programming language, and I was advised on vb.net. I have already...
7
by: MgGuigg | last post by:
Hello all, This is my first time posting a question to this forum, so here is hoping I am following protocol. I am scraping the rust off my old Basic programming skills, and have just recently...
6
by: Stefan Mueller | last post by:
After my web page has been loaded I'm doing some tests with a JavaScript. If I figure out that something is wrong I'd like to reload the whole frameset. With Internet Explorer and Mozilla Firefox...
0
by: noobcprogrammer | last post by:
#include "IndexADT.h" int IndexInit(IndexADT* word) { word->head = NULL; word->wordCount = 0; return 1; } int IndexCreate(IndexADT* wordList,char* argv)
32
by: BillJosephson | last post by:
Hi, I want to write a program in Java or C++, and just discovered on my bookshelve Codewarrior 7. It says Windows 95, 98, NT, 2000, ME. Can I develop a command line application that will work on...
3
by: Ratko | last post by:
Hi all, I have a python gui app that launches multiple applications using subprocess.Popen class and prints their output in the gui (using PIPEs, threads and wxPython). Everything works great...
3
by: Stef Mientki | last post by:
hello, I've a graphical application (wxPython), where the code in the main GUI loop is given below. 1 JAL_Loaded = False 2 while len(App_Running) 0: 3 if JALsPy_globals.State...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
1
by: coolsti | last post by:
Here is a strange occurrance: I start up my Visual C# 2008 Express program today, start a new project, put a bunch of controls on a form, save the project, and then close the program (for a break)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.