Re: [Py4j-users] Using copy.deepcopy() with Java Objects
Status: Beta
Brought to you by:
barthe
From: Barthelemy D. <ba...@cs...> - 2011-03-16 12:26:38
|
Hi, I'm afraid you won't be able to use Py4J for that. The Python side only keeps a reference to objects on the Java side (like an OID). The actual members or even the class definition is not stored on the Python side. In other words, objects are passed by reference, not by value. I believe RMI might be more suited because it serializes the objects but you need to implement the Remote interface (if I remember correctly). If you still want to use Python and Py4J, another alternative would be to serialize the object and just send the byte[] to the Python side. You could have something like: public interface StoreRestore1 { void store(String id, byte[] object); byte[] restore(String id); } or public interface StoreRestore2 { void store(String id, Serializable object); Serializable restore(String id); } In StoreRestore1, the Java side is responsible for serializing the object first before sending it to the Python object implementing the interface. In StoreRestore2, the Python object implementing the interface is responsible for serializing the object (probably by using the ObjectOutputStream/ObjectInputStream, ByteArrayOutputStream/ByteArrayInputStream). If you go with either of these solutions, you should use the latest code from the git repository because it treats byte[] as a primitive, i.e, if you send a byte[] from Java to Python, you will get a bytearray (or bytes in Python 3) on the Python side. Barthélémy On 03/16/2011 07:48 AM, Nicolò Perino wrote: > Hi, > actually what I am trying to do is a software transactional memory library in Python able to add transactional support to Java programs. > To simplify let's say that I would like to call a commit() operation in the Java program (for example a commit for each object) that call a Python routine that get the object from Python and store it safely, meaning that changes to the object in the Java-side don't reflect the effects in the object stored in the Python-side. Then I could call a restore() operation in the Java object, that take last saved object in the Python-side and replace the current instance of the Java object. > > I know it is a bit writhed or maybe useless, but it is just an experiment. > > bye > > Nicolò Perino > ______ > PhD Student @ USI - Faculty of Informatics > http://www.people.usi.ch/perinon/ > > On 16 Mar 2011, at 12:04, Barthelemy Dagenais wrote: > >> Hi Nicolò, >> >> as Alex says, it is not possible to use deepcopy with a Java object in >> Python and I'm not sure it makes sense anyway. Are you trying to >> dynamically duplicate a Java object graph from Python or...? >> >> With more information about what you are trying to do, we may be able to >> find an alternative. >> >> Barthélémy >> >> On 03/16/2011 06:50 AM, Alex Grönholm wrote: >>> 16.03.2011 12:24, Nicolò Perino kirjoitti: >>>> Hi, >>>> I was experimenting with your library P4JS and I was wandering how to create a copy of a Java object in Python environment. So I tried with the Python copy module, starting from your "stack" example: >>>> >>>>>>> stack = gateway.entry_point.getStack() >>>>>>> stack >>>> JavaObject id=o0 >>>>>>> import copy >>>>>>> a = copy.deepcopy(stack) >>>> Traceback (most recent call last): >>>> File "<stdin>", line 1, in<module> >>>> File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/copy.py", line 173, in deepcopy >>>> y = copier(memo) >>>> File "/Library/Python/2.6/site-packages/py4j/java_gateway.py", line 343, in __call__ >>>> args_command = ''.join([get_command_part(arg, self.pool) for arg in new_args]) >>>> File "/Library/Python/2.6/site-packages/py4j/protocol.py", line 208, in get_command_part >>>> command_part = REFERENCE_TYPE + parameter._get_object_id() >>>> AttributeError: 'dict' object has no attribute '_get_object_id' >>>> >>>> Is there a way to success in coping objects? >>> You'll have to do it on the Java side. The exact method depends on what >>> type you are trying to copy. >>>> Thank you. >>>> Bye >>>> >>>> >>>> Nicolò Perino >>>> ______ >>>> PhD Student @ USI - Faculty of Informatics >>>> http://www.people.usi.ch/perinon/ >>>> >>>> >>>> ------------------------------------------------------------------------------ >>>> Colocation vs. Managed Hosting >>>> A question and answer guide to determining the best fit >>>> for your organization - today and in the future. >>>> http://p.sf.net/sfu/internap-sfd2d >>>> _______________________________________________ >>>> Py4j-users mailing list >>>> Py4...@li... >>>> https://lists.sourceforge.net/lists/listinfo/py4j-users >>> >>> >>> ------------------------------------------------------------------------------ >>> Colocation vs. Managed Hosting >>> A question and answer guide to determining the best fit >>> for your organization - today and in the future. >>> http://p.sf.net/sfu/internap-sfd2d >>> _______________________________________________ >>> Py4j-users mailing list >>> Py4...@li... >>> https://lists.sourceforge.net/lists/listinfo/py4j-users >> >> >> ------------------------------------------------------------------------------ >> Colocation vs. Managed Hosting >> A question and answer guide to determining the best fit >> for your organization - today and in the future. >> http://p.sf.net/sfu/internap-sfd2d >> _______________________________________________ >> Py4j-users mailing list >> Py4...@li... >> https://lists.sourceforge.net/lists/listinfo/py4j-users > > > ------------------------------------------------------------------------------ > Colocation vs. Managed Hosting > A question and answer guide to determining the best fit > for your organization - today and in the future. > http://p.sf.net/sfu/internap-sfd2d > _______________________________________________ > Py4j-users mailing list > Py4...@li... > https://lists.sourceforge.net/lists/listinfo/py4j-users |