I am trying to create a hybrid application with python back-end and java GUI and for that purpose I am using jython to access the data from the GUI.
I wrote code using a standard Python 3.7.4 virtual environment and it worked "perfectly". But when I try to run the same code on jython it doesn't work so it seems that in jython some packages like threading are overwritten with java functionality.
My question is how can I use the threading package for example from python but in jython environment?
Here is the error:
Exception in thread Thread-1:Traceback (most recent call last):
File "/home/dexxrey/jython2.7.0/Lib/threading.py", line 222, in _Thread__bootstrap
self.run()
self._target(*self._args, **self._kwargs)
Since you have already decoupled the application i.e using python for backend and java for GUI, why not stick to that and build in a communication layer between the backend and frontend, this layer could either be REST or any Messaging framework.
I think one issue you might be facing is that you are running on Jython 2.7.0 (~Python 2.7 compatible) but your code is written for CPython 3.7.4. So you might be facing Python 2 vs 3 issues.
The threading module in Jython is indeed different from CPython i.e. it is making use of JVM threading. However the intension is to be API compatible with Python 2.7 code so if it doesn't run that should be considered a bug.
My first suggestion would be try Jython 2.7.1 it contains many fixes over 2.7.0 https://www.jython.org/download however I suspect it still might not work. Check if your code runs on CPython 2.7 https://www.python.org/downloads/release/python-2716/ if it does then that sounds like a Jython bug. Open a ticket with details to reproduce at https://bugs.jython.org/
Related
Can Python invoke the Java Framework?
I want to know whether a Python project can invoke a Java Framework, I find a Java Framework in GitHub, whether I can use it in my Python project?
Jython
Jython is one method of calling Java from python -- actually, you run your Python inside Java JVM. This gives you access to almost any Java that runs on JVM, but comes with many limitations.
Because Jython is running python inside the JVM, this gives you acess to almost any Java library. However, you are very restricted in what Python you can use: you can only use Python 2.7, and can import pure Python libraries only (compiled Python libraries with C will not run on Jython).
For an example of a project that uses Jython: Processing.py runs on Jython in order to access the Processing Java API and its ecosystem of Java libraries.
https://github.com/jdf/processing.py
Note that Jython 2 and its docs are quite old, and that the developers are uncertain if / when Jython 3 will be released.
https://github.com/jython/jython3
py4j
py4j is a different approach -- it is "A Bridge between Python and Java" and lets native python code access separate Java running in a separate JVM. Note however that the python and Java code must be running in parallel and communicating through a gateway interface. This is communication between separately running processes -- you are not spinning up a JVM from Python or inside Python.
For example: on the JVM side pass myObject to a new GatewayServer(myObject); on the Python side create a JavaGateway() Python object and use it to communicate with the Java myObject.
Normally Python and Java have their own interpreters/VM's and cannot be shared. It is possible to use Jython but has limitations (fe. python version and support/compatibility with other python packages).
The interpreter and JVM do not match: Java has strict typing, python not. Java compiles and run, python is an interpreter and can change code in runtime (if you want). These are extra challenges why putting all in a same environment is very complex.
There are possibilities like a client/server architecture, but the feasability depends on the level of the framework.
Most of the time low level frameworks are optimized to run directly inside your application process. Any loose coupling will introduce performance and security and compatibility issues. Just think about how reflection will work or multiple inheritance.
If it is a high level framework (fe able to run stand alone) it is more feasable to use some sort of client/server. But still you have to develop a lot for it.
Industry standard is just to implement the framework of your desire in the language you want, then you can get also all the benefits of your platform.
My major program is written in Python 2.7 (on Mac) and need to leverage some function which is written in a Java 1.8, I think CPython cannot import Java library directly (different than Jython)?
If there is no solution to call Java from CPython, could I integrate in this way -- wrap the Java function into a Java command line application, Python 2.7 call this Java application (e.g. using os.system) by passing command line parameter as inputs, and retrieve its console output?
regards,
Lin
If you have lot of dependcieis on Java/JVM, you can consider using Jython.
If you would like to develop a scalable/maintainable application, consider using microservices and keep Java and Python components separate.
If your call to Java is simple and it is easy to capture the output and failure, you can go ahead with this running the system command to invoke Java parts.
A number of open source projects have been written to enable calling Java from CPython, depending on your needs.
Pyjnius
Py4J
JPype forked (original JPype hasn't been updated in years)
jpy
ok, So i searched net for the possible implementation but all that I managed to find is Django projects implementation on Java platform through Jython. But I want to do the reverse, i.e. implement/integrate java project ( which in my case is SAIKU server ) on Django platform.
The question being, is it possible, and if yes, then kindly point me to the solution.
Thanking in advance =)
For your specific requirement, I would suggest using RESTFul API to access the Saiku Server.
However if you need to run Java Classes from Python.
Here are the options available for you:
JCC -- a C++ code generator for calling Java from C++/Python. It produces producing Python extensions which communicate via JNI with a Java virtual machine. As it implies, this would require compilations of every possible call. However this project is backbone of PyLucene project.
CodeMesh. C++ code generator for Java.
Py4J Python programs running in a Python interpreter to dynamically access Java objects in a Java Virtual Machine.
JPype allow python programs full access to java class libraries. It is done through interfacing at the native level in both Virtual Machines. However there are no recent development in this front.
In general, having an loosly coupled integration through REST or RCP would be easy to maintain than tightly coupled JNI based implementation.
There's no way to run Java within the Python runtime (which is what it sounds like you want). There are Java to Python "translators" available, but they're terrible. Honestly, if you need a Java server and Django to sit inside the same process for some reason, Jython is the way to go.
There are lots of options outside of that though, off the top of my head:
Implement Python bindings for your server (See PyLucene for an example)
Implement a socket server within your Java server that Python can talk to directly
There is some library called pymorphy written in python. Unfortunately, for java there is not any library with the similar functionality - natural language processing for Russian lang. So I need to invoke some methods of pymorphy library from Java code.
First I've tried to solve this problem with Jython. But I've spent 2 days and the goal was not accomplished because python modules cdb, bsddb3, sqlite are written in C and they will not work with Jython.
Now I want to run some python light-weight server with pymorphy for handling request from Java code.
How could I implement this kind of java-python interaction with the maximum production performance? Or is there more simple way to call python from java?
Try Jepp, "Java Embedded Python". http://jepp.sourceforge.net/
I haven't used it beyond small projects, but it works as advertised, allowing one to call CPython transparently from Java. If you have the opposite problem, needing to call Java from CPython, definitely check out JPype. I've used it extensively and it works very well.
I think these libraries (cdb, bsddb3, sqlite) has a jython implementation in https://code.google.com/p/django-jython/ check it out
Can Python code be used from Java using Jython, without modifying the Python code in a way which will prevent it from working correctly in CPython?
If yes, what steps would have to be taken (in the Java code)?
If not, what are the reasons that this cannot be done (so far)?
Python modules can depend on certain Python versions (e.g. Python 3 vs Python 2 and even may require a minimum Python version (e.g. 2.6) in case of using dedicated language features introduced in some Python version)
Python modules may depend on C extensions which won't work with Jython
Python modules may use CPython features that are not available in Jython
In general: most Python-only code should work with Jython - however like in all cases: you have to test, test, test. Good written modules provide unittests - so you should try to run the tests from Jython and see what's happening.