Created
August 21, 2018 13:31
-
-
Save mbursa/777fbb21198737a8e1f57fcc66739830 to your computer and use it in GitHub Desktop.
Routine to make a deep copy of RPyC remote objects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
Example: | |
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 | |
else: | |
return obj | |
#end if | |
#end def |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment