comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Running a simple Python from Ada program
Date: Fri, 3 Apr 2020 19:35:23 +0200
Date: 2020-04-03T19:35:23+02:00	[thread overview]
Message-ID: <r67s4q$16a3$1@gioia.aioe.org> (raw)
In-Reply-To: e5076a57-5f9d-43eb-bac9-1bf016237389@googlegroups.com

On 2020-04-03 18:57, Rego, P. wrote:
> Does someone would have a simplest as possible way to run a Python script from the Ada project?

There is no simple way doing that. Python has serious issues.

> The Python script in this case only has a print in a loop, with a sleep, so each iteration it should print the arguments.

Yes, this is what I am doing. I also keep state between iterations so 
that the called Python script could update an object and then get it 
back on the next iteration.

> The Python script I'm using is
> import sys
> import time
> 
> for index in range(10):
>      print(sys.argv)
>      time.sleep(1)
> 
> On approach I am trying is running this one as a batch script, so using
> import sys
> import time
> 
> with Text_IO;
> with Interfaces.C; use Interfaces.C;
> procedure systest2 is
>     function Sys (Arg : Char_Array) return Integer;
>     pragma Import(C, Sys, "system");
>     Ret_Val : Integer;
> begin
>     Ret_Val := Sys(To_C("python testpy.py arg1 arg2"));
>     
> end systest2;
> 
> The problem is that the execution blocks the script, meaning that the Python printouts are only printed at the end of the execution, at once.

Do not do that. There exist Python C API, which you should use:

    https://docs.python.org/3/c-api/index.html

> I know that there is a solution (to run Python from Ada) based on GNATCOLL, but I couldn't find any example to run it.

Well, as an alternative, you could take a look how MAX! Home Automation 
runs Python scripts.

    http://www.dmitry-kazakov.de/ada/max_home_automation.htm#5.1

It:

1. Loads Python DLL dynamically
2. Initializes Python environment
3. Compiles script file an loads it as a module into the Python environment
4. Calls a function from the module (in a loop)
5. Handles exceptions from Python
6. Makes some Ada subroutines callable from the Python script

I must warn you, it is complicated. The files py.ads/adb are Python 
bindings. Subdirectories Linux and Windows contain OS-dependent bodies 
of Python library loader py-load_python_library.adb. 
py-elv_max_cube.ads/adb is a module of Ada subroutines callable from Python.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  reply	other threads:[~2020-04-03 17:35 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-03 16:57 Running a simple Python from Ada program Rego, P.
2020-04-03 17:35 ` Dmitry A. Kazakov [this message]
2020-04-03 19:05   ` Rego, P.
2020-04-03 19:19     ` Rego, P.
2020-04-03 20:06       ` Dmitry A. Kazakov
2020-04-04  1:48       ` Dennis Lee Bieber
2020-04-03 23:49   ` Mikhail Terekhov
2020-04-04  2:55     ` Nasser M. Abbasi
2020-04-04  8:15     ` Dmitry A. Kazakov
2020-04-04 10:44 ` fabien.chouteau
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox