r/javahelp • • Dec 31 '20

Solved Performance cost of calling Python program from Java program

Not sure if this is the right place to post this but I want to call my python program from my Java program. Does anyone know the performance cost for repeatedly calling the python program through shell commands using Runtime.exec()?

There's a library that only has Python support so I was thinking of just creating a python program utilizing that library and then use my java program to call it to get the output.

Edit: thanks to everyone who reponded! Helped me a lot

20 Upvotes

24 comments sorted by

•

u/AutoModerator Dec 31 '20

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

16

u/seanprefect Grumpy Guy Who Remembers Java 2 Dec 31 '20

Trust me that is going to be a lot more headache than it's worth. Java has god knows how many libraries it's very likely there's a library in java that's similar

4

u/CycleOfPain Dec 31 '20

Hmm, that's unfortunate. Then I'll keep looking for a similar library that's in Java. Thanks.

4

u/pizzthepizz Dec 31 '20

Which library do u need?

5

u/CycleOfPain Dec 31 '20

I initially wanted to use this library: https://github.com/ranaroussi/yfinance

which gives me access to the Yahoo Finance API.

I found a similar one for Java: https://financequotes-api.com/#fxquote

but there were some methods in the python library that weren't in the java library so that's one of the reasons why I wanted to use the python one.

6

u/ickysticky Dec 31 '20

https://github.com/sstrickx/yahoofinance-api

But yahoo finance is technically deprecated. And as such is sometimes out of date or even in some cases wrong.

After shopping around a bit I personally landed on alpha vantage as a good free replacement. With this Java library https://github.com/crazzyghost/alphavantage-java

Though the api for most of these is so simple raw http calls aren’t very difficult

1

u/CycleOfPain Dec 31 '20

Awesome, I'll check it out

7

u/iddej Dec 31 '20

Oh a question I can answer! I wrote an app in Java to query the lyrics to the current playing spotify song, and I use a python script to actually get the data from spotify. So I use Runtime.exec() on a small script every .2s and there’s no noticeable delay, so you could theoretically use it (although my python script is strictly offline so you might have to account for the networking delay)

2

u/CycleOfPain Dec 31 '20

Thanks for your input. That is something similar I was going to do for my program. That doesn't seem too bad. I'll do some tests myself.

0

u/[deleted] Dec 31 '20

[deleted]

2

u/CycleOfPain Dec 31 '20

Ah, this is perfect. Thanks again!

5

u/ynvaser That Java Guy Dec 31 '20

Try running it on GraalVM. Afaik it supports both, and you could call Python through either the Graal SDK dependency from Java, or via the native script engine api (the one you can also use Nashorn from).

It's been a while since I've done this, so sorry about the lack of proper jargon.
Also, what do your programs do? You could expose a REST endpoint on your python app in certain use cases.

1

u/CycleOfPain Dec 31 '20

I'm still working out what I want my program to do and what the end goal is. On the surface it's going to display stock prices and other financial information in a "modern-looking" interface. There's a lot of other features as well that I would like to display but I'm still thinking of what I want to do.

I initially wanted the python program to get the data for me through the Yahoo Finance API and I wanted the Java program to parse the data and display it in a GUI via JavaFX.

1

u/ynvaser That Java Guy Dec 31 '20

Why can't you just pull it from Java?

1

u/CycleOfPain Dec 31 '20

I did find a YahooFinance API for Java but it didn't have all of the features that come with the API for Python.

3

u/01110101_00101111 Jan 01 '21

If you absolutely need to use a Python and not Java library, I would recommend trying something like either Jython which runs on any JVM or switching entirely to GraalVM. You can also try using bindings for CPython to Java like Jep or jpy which use the standard CPython interpreter .

2

u/proskillz Some Skillz Dec 31 '20

Personally, I wouldn't do this. It might work, but it also may drive you insane.

First question is, why not just write the whole thing in Python? The second is, why not use Flask and set up two micro services? Use http instead of system calls to get what you need from the python service.

1

u/CycleOfPain Jan 01 '21

Yah, I don’t want to complicate things. I’ll probably go for another API or something.

You’re right. I could just use python entirely for what I’m doing. I’m a little stubborn and just want to work with Java. I don’t see myself working as a python developer either so I don’t really have motivation to learn Python (although it wouldn’t take me long to do so) and flask (or django). I kinda just want to stick with Java. I’m currently learning JavaFx and then I’m going to learn JSP and Spring. This project will utilize JavaFX

1

u/gargar070402 Jan 01 '21

Trust me man, if what you're doing is straightforward, it's ABSOLUTELY worth it to learn basic Python and Flask, especially given you have programming experience already. You're not locking yourself to using Python for the rest of your life, and it never hurts to know something useful.

4

u/aenigmaclamo Extreme Brewer Dec 31 '20

Having two languages interoperate is more headache than it's worth.

If you must, you should consider exposing the functionality over rest, grpc, or some other means using the network stack rather than executing something manually to adopt a microservice architecture. This allows you to get more consistent performance as it doesn't involve startup time and it decouples the two applications.

2

u/sweetno Dec 31 '20

Runtime.exec performance is worse on Windows, I've heard.

2

u/m1ss1ontomars2k4 Jan 01 '21

Starting a new program is often relatively expensive; it doesn't matter much that one is Python and one is Java.

Interop between 2+ different languages in one binary is sometimes expensive and sometimes tricky, but it won't have nearly as much performance penalty.

2

u/derpadurp Jan 01 '21

I have a java program that calls 6-7 different Python programs every couple hours throughout the day.

It’s been running for over 5 years in a business setting 24/7/365 and has never had an issue.

Just want to mention this because it’s not my opinion and I don’t oppose that of the majority of users here who will say this is foolish.

This is just my observation.

1

u/nutrecht Lead Software Engineer / EU / 20+ YXP Jan 02 '21

Does anyone know the performance cost for repeatedly calling the python program through shell commands using Runtime.exec()?

You're getting a lot of wrong answers simply because you're not explaining what the performance requirements are. There's an overhead of starting a new process, but if it's just something you run every second or so it's no big deal. If it's some kind of website with hundreds of users doing this in parallel; it's a whole different cup of tea.

If it's the first you're fine. If it's the second you should not do this.