[C++-sig] Combining Boost Serialization with Boost Python

James Amundson amundson at fnal.gov
Wed Mar 14 21:37:53 CET 2012


I have a use case involving Boost Serialization and Boost Python. I have 
a (mostly) C++ library wrapped with Python. The C++ code uses Boost 
Serialization to periodically create checkpoints from which the user can 
resume later. Getting the serialization and python libraries to work 
together didn't present any special problems until I had a case in which 
the user had a derived Python class (from a C++ base). In searching the 
web, I have seen people asking about similar cases, but never an answer. 
See, e.g.,
http://stackoverflow.com/questions/7289897/boost-serialization-and-boost-python-two-way-pickle 
<http://stackoverflow.com/questions/7289897/boost-serialization-and-boost-python-two-way-pickle>
.

I have a partial solution to the problem using Boost Serialization and 
Python Pickle together. I would appreciate any advice on how to improve 
it. My solution is limited in that the state of the base C++ object is 
not restored. I also get an extra copy of the C++ object. Since my 
current use case has no state in the base object, I can live with these 
limitations. I would however, like to find a full solution.

In the files I have attached, I can successfully run three variations on 
a simple test case. A class of type Base is passed to a class of type 
Caller. Caller calls the passed class's doit method twice, saves 
(checkpoints) its state, then calls the doit method again. The resume 
script restores the state of the Caller and Base classes, then calls the 
doit method again. If successful, the resumed doit call should be the 
same as the third doit call from the original script.

In testcheckpoint1.py, the Base class is the original (C++) Base. It has 
no state, so the example is pretty trivial. Its doit method prints 
"Base::doit"
--------------------------------------------------------------------------
% python testcheckpoint1.py
Base::doit
Base::doit
checkpointed here
Base::doit
% python testresume.py
resuming...
Base::doit
--------------------------------------------------------------------------

In testcheckpoint2.py I pass a derived Python class to Caller. The 
derived doit method displays "Derived:doit" and a counter:
--------------------------------------------------------------------------
% python testcheckpoint2.py
Derived::doit count = 1
Derived::doit count = 2
checkpointed here
Derived::doit count = 3
% python testresume.py
resuming...
Derived::doit count = 3
--------------------------------------------------------------------------

In testcheckpoint3,py, I use a derived Python class with a non-trivial 
constructor, which is used to set the initial value of the counter:
--------------------------------------------------------------------------
% python testcheckpoint3.py
Derived2::doit count = 5
Derived2::doit count = 6
checkpointed here
Derived2::doit count = 7
% python testresume.py
resuming...
Derived2::doit count = 7
--------------------------------------------------------------------------

As you can see, many things are possible with the code the way it 
currently exists. I would still like to figure out how to preserve the 
state of the base class. Any and all advice will be appreciated.

--Jim Amundson
<http://stackoverflow.com/questions/7289897/boost-serialization-and-boost-python-two-way-pickle> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120314/e7f1abe9/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mymodule.cc
Type: text/x-c++src
Size: 3548 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120314/e7f1abe9/attachment-0001.cc>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testcheckpoint1.py
Type: text/x-python
Size: 180 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120314/e7f1abe9/attachment-0005.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testcheckpoint2.py
Type: text/x-python
Size: 209 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120314/e7f1abe9/attachment-0006.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: derivedmodule.py
Type: text/x-python
Size: 844 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120314/e7f1abe9/attachment-0007.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testcheckpoint3.py
Type: text/x-python
Size: 211 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120314/e7f1abe9/attachment-0008.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testresume.py
Type: text/x-python
Size: 95 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120314/e7f1abe9/attachment-0009.py>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: Makefile
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120314/e7f1abe9/attachment-0001.ksh>


More information about the Cplusplus-sig mailing list