comp.lang.ada
 help / color / mirror / Atom feed
* Running a simple Python from Ada program
@ 2020-04-03 16:57 Rego, P.
  2020-04-03 17:35 ` Dmitry A. Kazakov
  2020-04-04 10:44 ` fabien.chouteau
  0 siblings, 2 replies; 10+ messages in thread
From: Rego, P. @ 2020-04-03 16:57 UTC (permalink / raw)


Does someone would have a simplest as possible way to run a Python script from the Ada project?

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

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. 

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.

Tnx

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Running a simple Python from Ada program
  2020-04-03 16:57 Running a simple Python from Ada program Rego, P.
@ 2020-04-03 17:35 ` Dmitry A. Kazakov
  2020-04-03 19:05   ` Rego, P.
  2020-04-03 23:49   ` Mikhail Terekhov
  2020-04-04 10:44 ` fabien.chouteau
  1 sibling, 2 replies; 10+ messages in thread
From: Dmitry A. Kazakov @ 2020-04-03 17:35 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Running a simple Python from Ada program
  2020-04-03 17:35 ` Dmitry A. Kazakov
@ 2020-04-03 19:05   ` Rego, P.
  2020-04-03 19:19     ` Rego, P.
  2020-04-03 23:49   ` Mikhail Terekhov
  1 sibling, 1 reply; 10+ messages in thread
From: Rego, P. @ 2020-04-03 19:05 UTC (permalink / raw)


> Do not do that. There exist Python C API, which you should use:
> 
>     https://docs.python.org/3/c-api/index.html

You mean that the problem is with the Python "print" ?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Running a simple Python from Ada program
  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
  0 siblings, 2 replies; 10+ messages in thread
From: Rego, P. @ 2020-04-03 19:19 UTC (permalink / raw)


> > Do not do that. There exist Python C API, which you should use:
> >     https://docs.python.org/3/c-api/index.html
> You mean that the problem is with the Python "print" ?

I tested with a very simple C code. If the Python print is blocked, it should be blocked also from a C call, I presume (ok someone will contest, anyway). Any case, from the C call, it does not block.

So I will try to simplify a bit the question. I want to do the equivalent of this

    #include <stdio.h>
    #include <stdlib.h>
    int main(){
      system("python testpy.py ddddd");
      return 0;
    }
In this case it does not block the testpy.py printouts.

But I am doing this
    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;
which blocks the testpy.py script until its end. This should not happen. So, please, how could I fix this?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Running a simple Python from Ada program
  2020-04-03 19:19     ` Rego, P.
@ 2020-04-03 20:06       ` Dmitry A. Kazakov
  2020-04-04  1:48       ` Dennis Lee Bieber
  1 sibling, 0 replies; 10+ messages in thread
From: Dmitry A. Kazakov @ 2020-04-03 20:06 UTC (permalink / raw)


On 2020-04-03 21:19, Rego, P. wrote:
>>> Do not do that. There exist Python C API, which you should use:
>>>      https://docs.python.org/3/c-api/index.html
>> You mean that the problem is with the Python "print" ?
> 
> I tested with a very simple C code. If the Python print is blocked, it should be blocked also from a C call, I presume (ok someone will contest, anyway). Any case, from the C call, it does not block.
> 
> So I will try to simplify a bit the question. I want to do the equivalent of this
> 
>      #include <stdio.h>
>      #include <stdlib.h>
>      int main(){
>        system("python testpy.py ddddd");
>        return 0;
>      }
> In this case it does not block the testpy.py printouts.
> 
> But I am doing this
>      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;
> which blocks the testpy.py script until its end. This should not happen.

It must, actually:

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem?view=vs-2019

> So, please, how could I fix this?

You need spawn python in an asynchronous process or else a batch script 
with something like "call python test.py ddddd" inside.

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Running a simple Python from Ada program
  2020-04-03 17:35 ` Dmitry A. Kazakov
  2020-04-03 19:05   ` Rego, P.
@ 2020-04-03 23:49   ` Mikhail Terekhov
  2020-04-04  2:55     ` Nasser M. Abbasi
  2020-04-04  8:15     ` Dmitry A. Kazakov
  1 sibling, 2 replies; 10+ messages in thread
From: Mikhail Terekhov @ 2020-04-03 23:49 UTC (permalink / raw)


On Friday, April 3, 2020 at 1:35:26 PM UTC-4, Dmitry A. Kazakov wrote:
> 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.
> 
Just curious, what serious issues?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Running a simple Python from Ada program
  2020-04-03 19:19     ` Rego, P.
  2020-04-03 20:06       ` Dmitry A. Kazakov
@ 2020-04-04  1:48       ` Dennis Lee Bieber
  1 sibling, 0 replies; 10+ messages in thread
From: Dennis Lee Bieber @ 2020-04-04  1:48 UTC (permalink / raw)


On Fri, 3 Apr 2020 12:19:44 -0700 (PDT), "Rego, P." <pvrego@gmail.com>
declaimed the following:

>
>So I will try to simplify a bit the question. I want to do the equivalent of this
>
>    #include <stdio.h>
>    #include <stdlib.h>
>    int main(){
>      system("python testpy.py ddddd");
>      return 0;
>    }
>In this case it does not block the testpy.py printouts.
>
>But I am doing this
>    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;
>which blocks the testpy.py script until its end. This should not happen. So, please, how could I fix this?

	system() block until the invoked command completes... That happens for
both C and via the Ada definition.

	However, output via python print() might be getting buffered if the
spawned command does not see stdout as a console -- which might be a result
of not having the C I/O environment initialized...

	Try adding the -u option to the invocation
https://docs.python.org/3/using/cmdline.html#miscellaneous-options
"""
-u

    Force the stdout and stderr streams to be unbuffered. This option has
no effect on the stdin stream.

    See also PYTHONUNBUFFERED.

    Changed in version 3.7: The text layer of the stdout and stderr streams
now is unbuffered.
"""

EG: "python -u testpy.py arg1 arg2"


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed@ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Running a simple Python from Ada program
  2020-04-03 23:49   ` Mikhail Terekhov
@ 2020-04-04  2:55     ` Nasser M. Abbasi
  2020-04-04  8:15     ` Dmitry A. Kazakov
  1 sibling, 0 replies; 10+ messages in thread
From: Nasser M. Abbasi @ 2020-04-04  2:55 UTC (permalink / raw)


On 4/3/2020 6:49 PM, Mikhail Terekhov wrote:
> On Friday, April 3, 2020 at 1:35:26 PM UTC-4, Dmitry A. Kazakov wrote:
>> 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.
>>

> Just curious, what serious issues?
> 

For me, it is  Duck typing :)

"Using Duck Typing, we do not check types at all."

https://www.geeksforgeeks.org/duck-typing-in-python/

But this is another issue I suppose than what is being discussed here.

--Nasser

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Running a simple Python from Ada program
  2020-04-03 23:49   ` Mikhail Terekhov
  2020-04-04  2:55     ` Nasser M. Abbasi
@ 2020-04-04  8:15     ` Dmitry A. Kazakov
  1 sibling, 0 replies; 10+ messages in thread
From: Dmitry A. Kazakov @ 2020-04-04  8:15 UTC (permalink / raw)


On 2020-04-04 01:49, Mikhail Terekhov wrote:
> On Friday, April 3, 2020 at 1:35:26 PM UTC-4, Dmitry A. Kazakov wrote:
>> 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.
>>
> Just curious, what serious issues?

Loading the Python library and initializing the environment is 
challenging under Windows. Tasking is a problem, cleaning up is a 
problem, some of C API functions do not work.

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Running a simple Python from Ada program
  2020-04-03 16:57 Running a simple Python from Ada program Rego, P.
  2020-04-03 17:35 ` Dmitry A. Kazakov
@ 2020-04-04 10:44 ` fabien.chouteau
  1 sibling, 0 replies; 10+ messages in thread
From: fabien.chouteau @ 2020-04-04 10:44 UTC (permalink / raw)


On Friday, April 3, 2020 at 6:57:16 PM UTC+2, Rego, P. wrote:
> Does someone would have a simplest as possible way to run a Python script from the Ada project?

Maybe this could help: https://github.com/AdaCore/gnatcoll-bindings/tree/master/python

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2020-04-04 10:44 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-03 16:57 Running a simple Python from Ada program Rego, P.
2020-04-03 17:35 ` Dmitry A. Kazakov
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

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