Have a look at

  https://docs.python.org/2/extending/embedding.html

The following does work for me

1. Create a test.c file with
{{{
#include <Python.h>

int
main(int argc, char *argv[])
{
  Py_SetProgramName("python");  /* optional but recommended */
  Py_Initialize();
  PyRun_SimpleString("from sage.all import *\n"
                     "print ZZ(3).is_prime()\n");
  Py_Finalize();
  return 0;
}
}}}

2. Then to compile you need to do

$ sage -sh
$ gcc $(python2-config --cflags) $(python2-config --ldflags) test.c

3. Run the program (still in the sage-sh shell)

$ ./a.out
True

4. Exit sage environment

$ exit

Vincent

On 10/01/2018 12:22, Berkeley Churchill wrote:
As part of a research project we're using sage as a subroutine for some
matrix computations over rings.  We have hundreds or thousands of these
computations, but each computation is fairly quick.  Right now, for each
computation we write python code into a .sage file, and then start a new
process with "sage somefile.sage > output".  This works fine, except each
time we incur the startup time, which is about 2.5 seconds (whereas running
a tiny python program is < 0.02s).  Since we call sage hundreds of times,
this adds up pretty quickly.

I'm wondering if there's a better way to do this.  Is there a recommended
practice?  Otherwise, is it possible to start sage in one process and then
reuse it?  Or, if there's a way to start up sage quickly (comparable to
python's start time) and then load the modules we need, that would be cool
too.  I'm hoping for a solution that doesn't require many changes to our
code or setting up server infrastructure.  We also want every computation
to be independent of the others.  For instance, we don't want an exception
in one computation to cause problems for a future one.  Any suggestions are
appreciated.  If it makes any difference, our code base is C++.


--
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to