Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SharedInterpreter hangs at instantiation #352

Open
MattNot opened this issue Nov 26, 2021 · 3 comments
Open

SharedInterpreter hangs at instantiation #352

MattNot opened this issue Nov 26, 2021 · 3 comments

Comments

@MattNot
Copy link

MattNot commented Nov 26, 2021

Describe the bug
Hi, I tried a simple integration with SpringBoot. The problem is that when i instatiate the SharedInterpreter it never comes back to the original thread.

To Reproduce

log.info("Before ");
Interpreter in = new SharedInterpreter();
in.exec("print('hi!')");
in.exec("a = 5");
Object res = in.getValue("a");
log.info(res.toString());
log.info("End");

The output of this is only "Before" and infact my postman request does never end.

I'm on Windows 10, setted the -Djava.library.path=C:\Python39\Lib\site-packages\ and done "pip install jep".

Am I missing something obvious?

Expected behavior
Return to the main thread after the execution

Environment (please complete the following information):

  • Win 10
  • Python 3.9
  • Java 11
  • Jep Version: latest (4.0.0) (imported with maven)
  • Python packages used (e.g. numpy, pandas, tensorflow): none with this example but i'll need pandas and tensorflow in the future
@ndjensen
Copy link
Member

No, you're not missing anything obvious. The print in Python might be buffered but I would still expect it to return and your later log infos to happen. Have you tried working with the jep script (jep.bat on Windows)? I'm just curious if that works or not on your machine. Also since it hangs when you instantiate the SharedInterpreter, is it possible for you to attach a debugger and step into that and find the exact line within the Jep source that is hanging?

@MattNot
Copy link
Author

MattNot commented Nov 29, 2021

Hi!
Did not try with jep.bat 'cause with pip install jep i did not get it.
Tried with the debugger, the origin of the issues seeams to be the line 85 of Jep.class costructor

MainInterpreter var3 = MainInterpreter.getMainInterpreter();

When entering in this and goes to

protected void initialize() 

method of MainInterpreter

it launches the

this.thread = new Thread("JepMainInterpreter")

And then... the void, at least for Intellij IDEA Ultimate debugger.

@ndjensen ndjensen changed the title SharedInterpreter does nothing SharedInterpreter hangs at instantiation Nov 29, 2021
@ndjensen
Copy link
Member

Ok, I've never installed through pip on Windows, but on Linux and macOS the jep script ends up in the python/bin directory. So jep.bat might be in the equivalent directory on Windows, wherever the Python executable is located.

So I'm not sure why it appears to hang on the new Thread() line. What I think is happening, and I could be wrong, is on line 184 we call this.wait() and wait for Python to initialize, which is going to happen on the other thread that was just started a few lines above. That other thread, in the code just above, initializes Python and then calls this.notify() to allow everything to proceed again. If that other thread never completes, it will hang. Why that code wouldn't complete or throw an exception, I don't know.

If you have time, you could download the Jep source and build and install from source (which is what pip is doing anyway so it should result in the same thing). Then you could add some printf("log") statements to pyembed.c's pyembed_startup() method starting around here to try and trace which line is hanging in the C code that gets the embedded Python all set up. Then we could go onward from there. If you do do that, to build and install from source it's just python setup.py build and python setup.py install.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants