Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Routine to make a deep copy of RPyC remote objects.
def rpyc_deep_copy(obj):
Makes a deep copy of netref objects that come as a result of RPyC remote method calls.
When RPyC client obtains a result from the remote method call, this result may contain
non-scalar types (List, Dict, ...) which are given as a wrapper class (a netref object).
This class does not have all the standard attributes (e.g. dict.tems() does not work)
and in addition the objects only exist while the connection is active (are weekly referenced).
To have a retuned value represented by python's native datatypes and to by able to use it
after the connection is terminated, this routine makes a recursive copy of the given object.
Currently, only `list` and `dist` types are supported for deep_copy, but other types may be
added easily.
Note there is allow_attribute_public option for RPyC connection, which may solve the problem too,
but it have not worked for me.
s = rpyc.connect(host1, port)
result = rpyc_deep_copy(s.root.remote_method())
# if result is a Dict:
for k,v in result.items(): print(k,v)
if (isinstance(obj, list)):
copied_list = []
for value in obj: copied_list.append(rpyc_deep_copy(value))
return copied_list
elif (isinstance(obj, dict)):
copied_dict = {}
for key in obj: copied_dict[key] = rpyc_deep_copy(obj[key])
return copied_dict
return obj
#end if
#end def
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment