Giter Site home page Giter Site logo

modulepickle's Introduction

This is not yet stable

modulepickle extends cloudpickle and adds support for pickling whole packages from your working directory. This is useful when you're developing those modules and don't want to manually ship them out every time you make a remote function call.

WARNING: If your code defers it's imports - if there are import statements hidden inside functions - you risk nasal demons.

Installation

Install with

pip install modulepickle

then anywhere in your code, call one of

import modulepickle; modulepickle.extend_ray()  # to extend the Ray pickler
import modulepickle; modulepickle.extend_cloudpickle()  # to extend the CloudPickle pickler

Demo

The easiest way to test things out is with Docker. Once you have it installed, install the libraries that the test file uses with

pip install cloudpickle docker

Then if you run something like

import other  # or any package in your working directory 
import modulepickle.test

def f():
    assert other.__name__ == 'other'
    
modulepickle.test.test(f)

the function f will be pickled along with the directory containing the module other, and the whole thing will be shipped to - and executed in - the docker container.

Internals

modulepickle works by changing how modules are serialized. In both regular pickle and cloudpickle, modules are serialized with just a name, and it's the unpickler's job to figure out what that name refers to. For modules that are fixed over long periods - like your numpy install - that's just fine, and any old dependency manager will ensure that the module that's used on one end is the same used on the other.

For modules that change frequently though - like the ones in your working directory - rebuilding and redeploying a bunch of containers every time you fix a bug is a pain in the ass. modulepickle's way around this is to send not just a reference to a module, but the entire package from your working directory. This would be wasteful in terms of bandwidth and local compute, except that in domains like machine learning evaluating the function itself is usually far, far more expensive that anything you care to do with your source tree.

So, that's what goes into the pickle. What happens on the other end? Well, little do most callers of pickle.dump know, but there's a whole VM down there. When the module is unpickled, it gets unzipped into a temporary directory whose name holds a hash of the directory's contents. This directory is added to the path, and from thereon out good ol' import works as you expect.

The more interesting part is when the next function gets unpickled. If the working directory it references is the same as before, then the old temp dir is reused. If it's different though, then all the modules referencing the old dir get purged from sys.modules, the old temp dir gets removed from the path, and a new temp dir containing a copy of the new working directory is added. As long as there are no deferred imports of modules hanging around in some dusty subfunction (and there shouldn't be!), this should all be cheery - the old function will hold onto it's references to the old modules, and the new function will get to import the new modules. If your code does defer it's imports though, it might end up importing after the temp dirs have been switched out, meaning your old function could import new code, meaning nasal demons.

Notes

This is the end result of several more complex approaches to the same problem. Originally I wanted to make full use of the extensibility in Python's import system and write something that, when unpickled, would insert itself into sys.meta_path and handle the unpickling from there. This will still probably work, but it turns out writing bug-free loaders and importers is a massive pain, and it's much easier to build off of the import system that already exists.

modulepickle's People

Contributors

andyljones avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

pag yangw1234

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.