Hi all,

I'm on a team that has a Python/C++ hybrid application -- Python
frontend calling into a C++ backend.  In what I imagine to be a common
use case for Julia, we are experimenting with rewriting the C++
backend in Julia; but while the rewrite itself went well, we're having
trouble getting the Python frontend to call the draft Julia backend.
Where by "having trouble" I mean nothing we've tried worked.  Has
anyone on this list successfully does this?  How?

Here is the context, the set of things we looked at, and what
happened.

Context:
- Everything is on Ubuntu 12.04
- We have both 32-bit and 64-bit machines, but most experimentation
  has been on 32-bit machines
- We are building dev Julia from source (pulled around two days ago)

Attempts:

1) We looked briefly at using PyCall, but all the documentation
describes calling Python from Julia, so we haven't tried it.  We would
love to know if someone has had success calling Julia from Python with
PyCall.

2) The Embedding Julia [1] page looked promising.  It describes
calling Julia from C, but since we already had a link from our Python
frontend to our C++ backend via Boost::Python, we decided to try it.
- At first, it completely didn't work.  So, after grovelling the
  Internet for a while, we figured that the best thing to do was to
  fork Julia and merge https://github.com/JuliaLang/julia/pull/4997 in
  our fork.
- Ultimately, the embedding.c example mentioned on said Embedding
  Julia page built and ran successfully, but our attempts to get the
  same code to run in our context failed with mysterious segfaults. [2]
  Our best guess is that the problem is that julia.h is actually a C
  header file, and attempting to include it form a C++ header file is
  producing disasters; but I, at least, do not have the chops to have
  already worked around that.
Has anybody successfully called Julia from Python via a C++ shim and
Boost::Python?  What do you have to do for that to work?

3) I imagine it's possible to use one of the ways to call C (as
opposed to C++) from Python, and then call Julia from C, but that
seemed even more complicated, so we haven't tried it.  Any success on
this path?

4) Said Python frontend is only ~4K line of code, plus ~3K lines of
tests; the main libraries it uses are a parsing library and HTTP
(client and server).  Would it be simpler to just rewrite it in
Julia as well?

Constraints we have (in case any of these are difficult):
- We would like to maintain a long-lived Julia object, keep it from
  being garbage-collected, and repeatedly call from Python various
  Julia functions that operate on it.
- We would like to pass structured data across those calls in both
  directions (e.g., nested lists and dicts on the Python side; but we
  can write custom code to build up and destructure corresponding
  Julia data).
- We don't actually need to pass callbacks in either direction.

Thank you in advance for your help,
~Alexey Radul

[1] http://docs.julialang.org/en/latest/manual/embedding/

[2] GDB said something like
 (gdb) print jl_unbox_float64(fl_box_float64(2.0))
 #1 1563740
 (gdb) print jl_unbox_float64(fl_box_float64(2.0))
 #2 1563780
Sorry, I don't have the actual number, but it was a large and changing
integer instead of the 2 I expected.

Reply via email to