How to marshal a function?

Cliff Wells logiplexsoftware at earthlink.net
Wed Nov 14 15:54:41 EST 2001


On Wednesday 14 November 2001 12:28, François Pinard wrote:
> Hello, my friends.
>
> Earlier today, I wondered how to marshal a function, in view of
> transmitting it to a remote machine and executing it there.  The idea was
> to not transmit Python source, when the byte-compilation has already been
> done on this side.
>
> I quickly found out that, for a given `function f(...) ...' I should
> transmit marshal-ed `f.func_code' instead of `f'.  The resulting code
> could even be eval'ed or exec'ed on the remote side, if the function
> does not require arguments.  I did not saw that I could usefully return
> something from the function, and surely many other properties are missing.

Try pickle (or cPickle) rather than marshal:

import pickle

def f(a, b, c):
    return a + b + c

pc = pickle.dumps(f)

# transmit pc to another machine...

f = pickle.loads(pc)

print f(1, 2, 3)


-- 
Cliff Wells
Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308
(800) 735-0555 x308




More information about the Python-list mailing list