NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.
To learn about what's going on, see About the Archive Site.
There are two big obstacles to running a JVM inside Python:* Threading model - it's very difficult to convince the JVM to shut down if it believes it has started a user interface and it's very difficult to convince the JVM to shut down its threads. You can't start the JVM on a daemon thread or convince Windows that a thread is a daemon thread. Python hangs on exit, waiting for JVM threads to terminate. * AWT - as Curtis says, the AWT and Python windowing toolkits battle over the UI thread under Mac OS X.
Our JavaBridge solution is perhaps the best solution for executing a JVM within Python. It lets you instantiate any Java object with Python garbage collection of the object when it goes out of scope and you can run any Java method and pass in any primitive or object type. There are helper methods for reflection and for wrapping Java objects. But see above.
I have used Python and Corba to connect Python to C# in a previous life (I've forgotten the libraries I used). That's somewhat hairy and involved and not a general solution (you have to rebuild both sides of everything when interfaces change). IMHO, a lightweight, application-specific protocol is best and a transport protocol such as TCP, HTTP or SOAP is your best shot at getting the data sent back and forth in a platform-neutral manner.
--Lee On 4/4/2012 10:55 AM, Curtis Rueden wrote:
Hi John & everyone, Depending on your application you could also wire Java and Python together using inter-process communication. There is a plethora of RPC libraries out there with good Python and Java support. E.g. protocol buffers.We faced the same problem trying to integrate Bio-Formats (a Java library; http://github.com/openmicroscopy/bioformats) and into CellProfiler (a Python application; http://github.com/CellProfiler).The first solution was in-process. However, there are some difficulties with launching and managing a JVM from native code: * You may not know a priori how much memory to assign to the Java max heap, and it cannot be changed after the JVM has launched. * Java never returns RAM to the system; the garbage collector just frees it internally for Java to reuse. * AFAIK, you cannot only ever start one JVM; even if it terminates, you cannot start a second one. * Unless you are very careful, Mac OS X has threading issues with Java AWT.Hence, the CellProfiler team developed a second solution, inter-process this time, called subimager:https://github.com/CellProfiler/subimagerSubimager is a lightweight HTTP server that can be queried from Python, which avoids most of the pitfalls mentioned above (though you still have to decide on a max heap size, of course).Lastly, CellProfiler abstracts the integration mechanism into a class that supports both in-process and inter-process, depending on the circumstances:https://github.com/CellProfiler/CellProfiler/blob/master/imagej/ijbridge.py It is now working very well! Maybe you can pursue a similar approach. For further reading, check out my "Interfacing from non-Java code" page: http://loci.wisc.edu/bio-formats/interfacing-non-java-codeIt is part of the Bio-Formats developer documentation but would be totally applicable to netCDF Java or any other Java library, too.Regards, CurtisOn Wed, Apr 4, 2012 at 9:18 AM, Don Murray <don.murray@xxxxxxxx <mailto:don.murray@xxxxxxxx>> wrote:I think John is looking for something that would allow C Python to call into the netCDF-Java libraries. For that, the two options seem to be: JPype JCC or using interprocess communication. Here's a good presentation on different approaches that I found on the web: http://www.slideshare.net/onyame/mixing-python-and-java I think Sean Arms at Unidata was looking at JCC at one point. Don On 4/4/12 7:38 AM, stephen.pascoe@xxxxxxxxxx <mailto:stephen.pascoe@xxxxxxxxxx> wrote: The important question is why you need Python. If it's to access some C library with python bindings (e.g. numpy or netCDF4-python) then Jython isn't appropriate and you'll need one of those Python<->bridges JPype or Jepp. If you need the very latest Python features (particularly Python 3) you'll also find Jython is a bit behind CPython. Many standard Python frameworks now work with Jython (e.g. Django, SQLAlchemy) but you'd have to check the compatibility of each dependency. Otherwise Jython is the most popular and well maintained choice. Depending on your application you could also wire Java and Python together using inter-process communication. There is a plethora of RPC libraries out there with good Python and Java support. E.g. protocol buffers. Stephen. --- Stephen Pascoe +44 (0)1235 445980 <tel:%2B44%20%280%291235%20445980> Centre of Environmental Data Archival STFC Rutherford Appleton Laboratory, Harwell Oxford, Didcot OX11 0QX, UK -----Original Message----- From: Robert Casey [mailto:rob@xxxxxxxxxxxxxxxxxxx <mailto:rob@xxxxxxxxxxxxxxxxxxx>] Sent: 04 April 2012 14:31 To: Pascoe, Stephen (STFC,RAL,RALSP); John Caron Cc: netcdf-java@xxxxxxxxxxxxxxxx <mailto:netcdf-java@xxxxxxxxxxxxxxxx>; thredds@xxxxxxxxxxxxxxxx <mailto:thredds@xxxxxxxxxxxxxxxx> Subject: Re: [thredds] Java to Python tools Hi John- If you use Jython, it looks to be pretty seamless. I am just starting to develop in Jython, so I can't claim experience in this department yet. The first section of this page details Java within Jython. http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html -Rob On Apr 4, 2012, at 1:02 AM,<stephen.pascoe@xxxxxxxxxx <mailto:stephen.pascoe@xxxxxxxxxx>> wrote: Hi John, Direct experience beyond playing, no; but I've come across several options: * Jpype : http://sourceforge.net/project/jpype -- embeds a JVM in Python * Jepp : http://jepp.soureforge.net -- embeds Python interpreter in Java * Jython : http://www.jython.org -- A Python implementation in the JVM. I expect you want Jpype as in your case Python is doing the wrapping. I tested it out once and it worked for me. The project seems isn't exactly active but it still appears to be in use. Cheers, Stephen. --- Stephen Pascoe +44 (0)1235 445980 <tel:%2B44%20%280%291235%20445980> Centre of Environmental Data Archival STFC Rutherford Appleton Laboratory, Harwell Oxford, Didcot OX11 0QX, UK -----Original Message----- From: thredds-bounces@xxxxxxxxxxxxxxxx <mailto:thredds-bounces@xxxxxxxxxxxxxxxx> [mailto:thredds-bounces@xxxxxxxxxxxxxxxx <mailto:thredds-bounces@xxxxxxxxxxxxxxxx> ] On Behalf Of John Caron Sent: 04 April 2012 00:20 To: Java NetCDF; THREDDS community Subject: [thredds] Java to Python tools Hi all: Anyone have experience with ways to wrap java in Python? We are thinking about how to solve this issue again. John _______________________________________________ thredds mailing list thredds@xxxxxxxxxxxxxxxx <mailto:thredds@xxxxxxxxxxxxxxxx> For list information or to unsubscribe, visit: http://www.unidata.ucar.edu/mailing_lists/ -- Scanned by iCritical. _______________________________________________ thredds mailing list thredds@xxxxxxxxxxxxxxxx <mailto:thredds@xxxxxxxxxxxxxxxx> For list information or to unsubscribe, visit: http://www.unidata.ucar.edu/mailing_lists/-- Don MurrayNOAA/ESRL/PSD and CIRES 303-497-3596 <tel:303-497-3596> http://www.esrl.noaa.gov/psd/people/don.murray/ _______________________________________________ netcdf-java mailing list netcdf-java@xxxxxxxxxxxxxxxx <mailto:netcdf-java@xxxxxxxxxxxxxxxx> For list information or to unsubscribe, visit: http://www.unidata.ucar.edu/mailing_lists/
thredds
archives: