Skip to content

Allow using PyCall with in-process symbols - #293

Merged
stevengj merged 2 commits into
JuliaPy:masterfrom
JuliaComputing:kf/inprocsyms
Jul 15, 2016
Merged

stevengj merged 2 commits into
JuliaPy:masterfrom
JuliaComputing:kf/inprocsyms

Conversation

@Keno

@Keno Keno commented Jul 7, 2016

Copy link
Copy Markdown
Collaborator

This is the companion pull request to JuliaPy/pyjulia#54 in order to allow PyCall to use the python symbols already present in the current process. This behavior is currently guarded by the PYCALL_CURRENT_PROCESS environment variable. It may be worth enabling the codepath even when that variable is not set, but my thinking was that since a good amount of special handling is required to make this work properly, it should only be enabled if explicitly requested.

@coveralls

coveralls commented Jul 7, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.7%) to 58.43% when pulling ba96fbb on JuliaComputing:kf/inprocsyms into ee0897b on stevengj:master.

Comment thread deps/build.jl Outdated
islink(path) && (path = readlink(path))
path
else
error("Unsupported operating system")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does /proc/self/exe work on freebsd etc?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Keno

Keno commented Jul 8, 2016

Copy link
Copy Markdown
Collaborator Author

Ok, I have got pycall basically working with this. There are two remaining (and related questions). They are probably more pyjulia questions, but let's discuss them here, since they affect how this PR will evolve.

The first most immediate problem is that deps.jl should be separate for pyjulia from the one for julia itself. Right now, one overrides the other which is annoying. Relatedly, I imagine people might want to use pyjulia from different python installations (e.g. python2/python3). In that case, we'd probably want to use the regular .ji files if your python matches the one selected by pyjulia, but otherwise create a separate .ji directory elsewhere. The question is where. In JuliaPy/pyjulia#54 I currently have one global such directory, but that obviously doesn't work if people want to use pyjulia with multiple versions of python. Does python have a directory where it is customary to put such things? Should we just version the directories by python version as well as julia version (e.g. pyjulia2.7-v0.5-static). Other suggestions?
Further, may it make sense to put the deps.jl file in the same directory as the .ji file since they're essentially tied together?

@stevengj

stevengj commented Jul 8, 2016 •

Copy link
Copy Markdown
Member

Maybe in Pkg.dir("PyCall", "deps", "pyjulia", "pythonX.Y")? It's a lot easier to work with a subdirectory of deps than some other random location in the directory tree, and that way it stays in sync with the PyCall version being used. (e.g. it will continue to work if someone makes a private clone of PyCall).

@stevengj

stevengj commented Jul 8, 2016 •

Copy link
Copy Markdown
Member

As for pyjulia's .ji file, I agree that this should also be in the same directory (under deps/pyjulia/pythonX.Y) as the deps.jl file, I think.

@Keno

Keno commented Jul 8, 2016

Copy link
Copy Markdown
Collaborator Author

That sounds good to me. Should there be an environment variable that determines which subdirectory to use?

@Keno

Keno commented Jul 8, 2016

Copy link
Copy Markdown
Collaborator Author

Well, the problem is that it has to create .ji files for all packages, not just PyCall, so having the .ji files under PyCall is a little weird.

@stevengj

stevengj commented Jul 8, 2016 •

Copy link
Copy Markdown
Member

I hate using environment variables for something like this. Can't PyCall.jl just check whether the current process has the Py_GetVersion symbol, as I suggested above, and if it does it can assume it is in pyjulia and call Py_GetVersion to determine the correct directory for deps.jl?

@Keno

Keno commented Jul 8, 2016

Copy link
Copy Markdown
Collaborator Author

Yes, it can. I was about to say you'd pay a cost at startup time, but that's only once during precompilation, so I think it's fine.

@tkelman

tkelman commented Jul 8, 2016

Copy link
Copy Markdown
Contributor

it has to create .ji files for all packages

Why is this?

@Keno

Keno commented Jul 8, 2016

Copy link
Copy Markdown
Collaborator Author

Because it has to do it for PyCall and all packages that depend on it and there's no way to tell julia to only do it for those packages.

@tkelman

tkelman commented Jul 8, 2016

Copy link
Copy Markdown
Contributor

I'd be a bit surprised if someone needed something that depends on PyCall from inside pyjulia.

@Keno

Keno commented Jul 8, 2016

Copy link
Copy Markdown
Collaborator Author

PyPlot?

@tkelman

tkelman commented Jul 8, 2016

Copy link
Copy Markdown
Contributor

Why would you go through Julia for that if you're embedding Julia inside Python?

@Keno

Keno commented Jul 8, 2016

Copy link
Copy Markdown
Collaborator Author

You wouldn't, but you might be using a package which has a dependency on it (quite a few packages do).

@tkelman

tkelman commented Jul 8, 2016

Copy link
Copy Markdown
Contributor

Should we try to do this in a way that the precompile system can recognize which configuration PyCall is in and just recompile the dependency ji's in place if the config changes?

@Keno

Keno commented Jul 8, 2016

Copy link
Copy Markdown
Collaborator Author

I'm open to any suggestions.

@Keno

Keno commented Jul 8, 2016

Copy link
Copy Markdown
Collaborator Author

I suppose since we're intercepting the precompile calls anyway, we could check if PyCall was loaded and if so change the output path. Devious but I somewhat like it. @vtjnash ?

@tkelman

tkelman commented Jul 8, 2016

Copy link
Copy Markdown
Contributor

I'd say something like a dummy include_dependency that switched between two choices depending whether we're in in-process or shared library mode. Probably set a module level constant too so it can be checked.

@Keno

Keno commented Jul 8, 2016

Copy link
Copy Markdown
Collaborator Author

@vtjnash is saying the general solution is overly complicated, but intercepting the precompile call and hardlinking should work fine.

@Keno

Keno commented Jul 9, 2016

Copy link
Copy Markdown
Collaborator Author

After ruminating on this some more, this is how I think I want this to work:

PyCall checks if Python symbols are present in the process and if so does not load any deps.jl, but figures out where the python library is, by looking at where the symbol came from (or alternatively using the python APIs to query this).

Then pyjulia does the following:

  • If deps.jl is present and the versions declared therein match the currently running python process (same version, dynamically linked), pyjulia just does what it has always done, which is load PyCall without any special precompile flags or anything.
  • If deps.jl is absent, or the versions don't match, it intercepts precompilation, sets up a separate cache directory and uses that. Since PyCall will prefer local symbols over deps.jl and we intercepted precompilation, this will create a working cache file. Slight caveat is that while precompiling PyCall only the separate cache directory can be on the load path, but thereafter both can be.

In the second case we also use the interception trick to figure out if PyCall was loaded and if not put the cache file in the original place (rather than the pyjulia private place) and simply symlink, thereby sharing cache files for packages that do not depend on PyCall.

@stevengj

stevengj commented Jul 9, 2016

Copy link
Copy Markdown
Member

That sounds workable.

@Keno
Keno force-pushed the kf/inprocsyms branch from ba96fbb to 1f62207 Compare July 9, 2016 18:59
@Keno

Keno commented Jul 9, 2016

Copy link
Copy Markdown
Collaborator Author

Ok, I've implemented that approach here and in pyjulia. Seems to work decently well. Needs a bit more testing and cleanup.

@coveralls

coveralls commented Jul 9, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.2%) to 59.273% when pulling 51e6f11 on JuliaComputing:kf/inprocsyms into ee0897b on stevengj:master.

@coveralls

coveralls commented Jul 9, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.05%) to 59.051% when pulling de036a4 on JuliaComputing:kf/inprocsyms into ee0897b on stevengj:master.

@Keno

Keno commented Jul 15, 2016

Copy link
Copy Markdown
Collaborator Author

@stevengj Are you ok with this generally? If so I'll rebase and clean a bit, after which this should be good to go.

@stevengj

Copy link
Copy Markdown
Member

LGTM.

@Keno

Keno commented Jul 15, 2016

Copy link
Copy Markdown
Collaborator Author

rebased.

@coveralls

coveralls commented Jul 15, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-1.2%) to 57.133% when pulling a645821 on JuliaComputing:kf/inprocsyms into df54414 on stevengj:master.

@coveralls

coveralls commented Jul 15, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.2%) to 58.501% when pulling a645821 on JuliaComputing:kf/inprocsyms into df54414 on stevengj:master.

@stevengj
stevengj merged commit 32c4f9a into JuliaPy:master Jul 15, 2016
@tkelman

tkelman commented Jul 15, 2016

Copy link
Copy Markdown
Contributor

be sure to bump at least minor when tagging this, there's a chance it may have unintended consequences for dependent packages (hopefully not but won't know until after it gets tagged)

@stevengj

Copy link
Copy Markdown
Member

Yup, it's a new feature anyway so it should get a minor bump.

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

Successfully merging this pull request may close these issues.

4 participants