Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you are serializing Python objects and not simple datatypes, and <em>have</em> to use pickle, try cPickle.HIGHEST_PROTOCOL:</p> <pre><code>my_serialized_object = cPickle.dumps(my_object, cPickle.HIGHEST_PROTOCOL) </code></pre> <p>The default protocol is compatible with older versions of Python, but you likely don't care about that.</p> <p>I just did a simple benchmark with a 1000 key dict and it was almost an order-of-magnitude faster.</p> <p><strong>UPDATE:</strong> Since you appear to already be using the highest protocol, you are going to have to do some extra work to get more performance. Here is what I would do at this point:</p> <ol> <li><p>Identify which classes are the slowest to pickle</p></li> <li><p>Create a pair of methods in the class to implement a faster serialization method, say _to_string() and _from_string(s). The actual serialization can be tailored to what the object encompasses and how it's going to be used. For example, some objects may really only contain a simple string, such as a rendered template, and some may actually be sent to a browser as JSON, in which case you can simply serialize to JSON and serve it directly. Use the <a href="http://docs.python.org/library/timeit.html" rel="nofollow">timeit module</a> to ensure that your method is actually faster</p></li> <li><p>In your decorator, check hasattr(object, '_to_string') and use that instead, if it exists</p></li> </ol> <p>This method lets you tackle the worst classes first, and introduces minimal disruption to the code base.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload