Skip to main content
# resume module

This module provide a support for saving and restoring the same interpreter
state in different executions saving the locals and some internal status
like random state in a checkpoint file.

The integration as been designed to be seamless:

1. just create a checkpoint at the begging of the function
2. set the 1st run time variables
3. call restore() before the evolutionary process
4. performs any computation cycles
5. call sync() or save() meanwhile as your convenience to store current state.

If the code is interrupted for an reason, the next execution will:

1. use the same checkpoint definition to locate the saved state.
2. define the same variables as initial placeholders
3. calling restore() will load these variables from disk
4. the computation cycles continue from last saved state.
5. the same sync() or save() will keep your step forward work save.

**Notes**:

- the name of the checkpoint is auto-magically selected from the function name.
- sync() will save state only if the elapsed time from last save if bigger
than a rate (passed in constructor)
- save() always dump state into disk
- use numpy.random module instead python random module due the lack of seed
recovery in python library.
On the contrary numpy provide a get_state() and set_state() for this propose.


## Abstract

In situations where we are developing an application or library that will be use to create long computation reports or results, we want to recover the progress of the computation in case of failure or environmental changes.

The integration with existing code must be seamless, hiding all the details to the user. Actually there is only 3 code lines you should insert in your existing code.

1. instantiate a checkpoint, usually at the begining of the function of code.
2. select the restoration point in case of re-run
3. decide when save the internal state within loop calculations.

## Show me an example

Let' say that we have this code for compute prime numbers:

```python
def compute_primes(n=10):
"compute n 1st primes"
# define initial state of the algorithm
i = 2
primes = [i]
candidate = primes[-1]

while n > 0:
candidate += 1
for p in primes:
if not candidate % p:
break
else:
primes.append(candidate)
n -= 1
return primes
```

We can reuse previous computation using checkpoint support.

The changes in the code are:

```python
def compute_primes(n=10):
"compute n next primes"
chp = Checkpoint()
# define initial state of the algorithm
i = 2
primes = [i]
candidate = primes[-1]

# restore previous work (if any)
chp.restore() # note that 'n' preserve the current calling value

# continue from last time (or initial state)
while n > 0:
candidate += 1
for p in primes:
if not candidate % p:
break
else:
primes.append(candidate)
n -= 1
# dump current state to disk. Next call will continue from here
chp.save()
return primes
```

and some code for testing

```python
>>> N=5
>>> primes_1 = compute_primes()
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
>>> primes_2 = compute_primes(n=N)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53]
>>> assert len(primes_1) > 0
>>> assert len(primes_2) == len(primes_1) + N
```

## Notes

- The checkpoint is stored in compressed pickle format
- Checkpoints preserve numpy random state to guarantee the same results as if the process will not be interrupted
- Checkpoints are stored in '.checkpoints/' hidden folder by default.
- checkpoints will be discarded if las update is beyond of CACHE_EXPIRE by default.
- checkpoints store all basic types and numpy types by default.
- The code is in alpha version, any comment of pull request is welcome.

## Install

```
$ pip install resume
```

or download and improve the code by yourself :) installing in develop mode in your home directory

```
python setup.py develop --user
```


## Python versions

Is tested only in python 2.7 yet, but there is not any deliberated incompatibility with python 3.x versions.


Release files for resume 0.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for resume 0.13
File Size Uploaded
resume-0.13.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for resume 0.13
File Interpreter ABI Platform
resume-0.13-py2-none-any.whl Python 2 none any Details

Total release size:14.8 kB

Release files / resume-0.13.tar.gz

Download URL resume-0.13.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
21790557acd43d398c2377143b7ab2fa5df38696402fe3d3bc8713c4b12a1f8a
BLAKE2b-256 checksum
How to use checksums
9148ad8ab4b6b52a767202944050512c3c5e4a988275fcb8b853983c9659c44a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / resume-0.13-py2-none-any.whl

Download URL resume-0.13-py2-none-any.whl
Size 8.9 kB
Tags Python 2
SHA-256 checksum
How to use checksums
bac238187198798a3cb0bec341bc4a2ab4638f57ca7ef0cce6c5b5cbf9009615
BLAKE2b-256 checksum
How to use checksums
5772ea697408ad9fcc695dc50d979fb92a908c84fd6b89c427eeb11198ab45dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.13 This release

2 release files

0.12

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page