title: How to pickle a python function with its dependencies?
body: <p>As a follow up to this question:
  <a href="https://stackoverflow.com/questions/1253528/is-there-an-easy-way-to-pickle-a-python-function-or-otherwise-serialize-its-cod">Is there an easy way to pickle a python function (or otherwise serialize its code)?</a></p>
  
  <p>I would like to see an example of this bullet from the above post:</p>
  
  <p>"If the function references globals (including imported modules, other functions etc) that you need to pick up, you'll need to serialise these too, or recreate them on the remote side. My example just gives it the remote process's global namespace."</p>
  
  <p>I have a simple test going where I am writing a functions byte code to a file using marshal:</p>
  
  <pre><code>def g(self,blah): 
      print blah
  
  def f(self):
      for i in range(1,5):
          print 'some function f'
          g('some string used by g')
  
  data = marshal.dumps(f.func_code)
  
  file = open('/tmp/f2.txt', 'w')
  file.write(data)
  </code></pre>
  
  <p>Then starting a fresh python instance I do:</p>
  
  <pre><code>file = open('/tmp/f2.txt', 'r')
  code = marshal.loads(file.read())
  func2 = types.FunctionType(code, globals(), "some_func_name");
  func2('blah')
  </code></pre>
  
  <p>This results in a:</p>
  
  <pre><code>NameError: global name 'g' is not defined
  </code></pre>
  
  <p>This is independent of the different approaches I have made to including g. I have tried basically the same approach to sending g over as f but f can still not see g. How do I get g into the global namespace so that it can be used by f in the receiving process?</p>
  
  <p>Someone also recommended looking at pyro as an example of how to do this. I have already made an attempt at trying to understand the related code in the disco project. I took their dPickle class and tried to recreate their disco/tests/test_pickle.py functionality in a standalone app without success. My experiment had problems doing the function marshaling with the dumps call. Anyway, maybe a pyro exploration is next.</p>
  
  <p>In summary, the basic functionality I am after is being able to send a method over the wire and have all the basic "workspace" methods sent over with it (like g).</p>
  
  <p>Example with changes from answer:</p>
  
  <p>Working function_writer:</p>
  
  <pre><code>import marshal, types
  
  def g(blah): 
      print blah
  
  
  def f():
      for i in range(1,5):
          print 'some function f'
          g('blah string used by g')
  
  
  f_data = marshal.dumps(f.func_code)
  g_data = marshal.dumps(g.func_code);
  
  f_file = open('/tmp/f.txt', 'w')
  f_file.write(f_data)
  
  g_file = open('/tmp/g.txt', 'w')
  g_file.write(g_data)
  </code></pre>
  
  <p>Working function_reader:</p>
  
  <pre><code>import marshal, types
  
  f_file = open('/tmp/f.txt', 'r')
  g_file = open('/tmp/g.txt', 'r')
  
  f_code = marshal.loads(f_file.read())
  g_code = marshal.loads(g_file.read())
  
  f = types.FunctionType(f_code, globals(), 'f');
  g = types.FunctionType(g_code, globals(), 'g');
  
  f()
  </code></pre>