🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

16 total messages Started by saito Wed, 14 Oct 2026 13:07
Python
#36
Author: saito
Date: Wed, 14 Oct 2026 13:07
8 lines
1413 bytes
I would like to start a python session, send it some simple commands and 
get the results back. From Tcl, I have tried opening a pipe with RDWR 
and r+ and various fconfigure/filevenets and none seem to work.

AI bot solutions end up hanging on the reading part as well.

Is this use case possible?
Re: Python
#37
Author: Arjen
Date: Wed, 14 Oct 2026 13:13
14 lines
1341 bytes
saito <saitology9@gmail.com> posted:

> I would like to start a python session, send it some simple commands and 
> get the results back. From Tcl, I have tried opening a pipe with RDWR 
> and r+ and various fconfigure/filevenets and none seem to work.
> 
> AI bot solutions end up hanging on the reading part as well.
> 
> Is this use case possible?
> 

You should probably have a look at Tohil - this makes the interconnection between 
Tcl and Python quite easy. See https://wiki.tcl-lang.org/page/Accessing+Tcl+and+Python+from+one+another
Re: Python
#38
Author: saito
Date: Wed, 14 Oct 2026 13:26
12 lines
1814 bytes
On 10/14/2026 9:13 AM, Arjen wrote:
> 
> You should probably have a look at Tohil - this makes the interconnection between
> Tcl and Python quite easy. See https://wiki.tcl-lang.org/page/Accessing+Tcl+and+Python+from+one+another

I'd try it but it looks like it doesn't run on Windows yet. I would like 
to avoid binary extensions if possible.

I am not sure how the file open modes RDWR and r+ are supposed to work. 
Perhaps my challenge is isolating the two ends of the pipe. I am pretty 
sure I have seen this being done but I am not sure where to start looking.
Re: Python
#39
Author: Rich
Date: Wed, 14 Oct 2026 13:53
18 lines
1869 bytes
saito <saitology9@gmail.com> wrote:
> On 10/14/2026 9:13 AM, Arjen wrote:
>> 
>> You should probably have a look at Tohil - this makes the interconnection between
>> Tcl and Python quite easy. See https://wiki.tcl-lang.org/page/Accessing+Tcl+and+Python+from+one+another
> 
> I'd try it but it looks like it doesn't run on Windows yet. I would like 
> to avoid binary extensions if possible.
> 
> I am not sure how the file open modes RDWR and r+ are supposed to work. 
> Perhaps my challenge is isolating the two ends of the pipe. I am pretty 
> sure I have seen this being done but I am not sure where to start looking.

Perhaps some of the discussion here:

https://wiki.tcl-lang.org/page/chan+pipe

will be helpful?
Re: Python
#41
Author: Arjen
Date: Wed, 14 Oct 2026 13:56
18 lines
1679 bytes
saito <saitology9@gmail.com> posted:

> On 10/14/2026 9:13 AM, Arjen wrote:
> > 
> > You should probably have a look at Tohil - this makes the interconnection between
> > Tcl and Python quite easy. See https://wiki.tcl-lang.org/page/Accessing+Tcl+and+Python+from+one+another
> 
> I'd try it but it looks like it doesn't run on Windows yet. I would like 
> to avoid binary extensions if possible.
> 
> I am not sure how the file open modes RDWR and r+ are supposed to work. 
> Perhaps my challenge is isolating the two ends of the pipe. I am pretty 
> sure I have seen this being done but I am not sure where to start looking.
> 
I have used it on Windows (not extensively I admit), but your method with pipes 
should work as well. Have you checked the Wiki for examples? (I see Rich has 
made a similar remark)
Re: Python
#40
Author: saito
Date: Wed, 14 Oct 2026 13:57
11 lines
1564 bytes
On 10/14/2026 9:53 AM, Rich wrote:
> 
> Perhaps some of the discussion here:
> 
> https://wiki.tcl-lang.org/page/chan+pipe
> 
> will be helpful?

Thank you very much! This is the command I was thinking of.
Now I will see if it works the way I need it.
Re: Python
#42
Author: saito
Date: Wed, 14 Oct 2026 14:03
18 lines
1866 bytes
On 10/14/2026 9:56 AM, Arjen wrote:
>
> I have used it on Windows (not extensively I admit), 

Interesting! I wonder if their info is simply out of date. Under their 
"To Do", it says: "We'd like to have Tohil work on Windows if someone 
would be willing to take it on."


> but your method with pipes
> should work as well. Have you checked the Wiki for examples? (I see Rich has
> made a similar remark)

I hope so too. That was indeed the command that I was trying to find. 
Sometimes you just don't know what magic words to use.

Thank you!
Re: Python
#43
Author: Arjen
Date: Wed, 14 Oct 2026 14:30
24 lines
1659 bytes
saito <saitology9@gmail.com> posted:

> On 10/14/2026 9:56 AM, Arjen wrote:
> >
> > I have used it on Windows (not extensively I admit), 
> 
> Interesting! I wonder if their info is simply out of date. Under their 
> "To Do", it says: "We'd like to have Tohil work on Windows if someone 
> would be willing to take it on."
> 
> 
> > but your method with pipes
> > should work as well. Have you checked the Wiki for examples? (I see Rich has
> > made a similar remark)
> 
> I hope so too. That was indeed the command that I was trying to find. 
> Sometimes you just don't know what magic words to use.
> 
> Thank you!
> 

If it does not work, can you show us via a simple/minimal example 
how you are trying to do it?
Re: Python
#45
Author: Ralf Fassel
Date: Wed, 14 Oct 2026 16:04
18 lines
1656 bytes
* saito <saitology9@gmail.com>
| I would like to start a python session, send it some simple commands
| and get the results back. From Tcl, I have tried opening a pipe with
| RDWR and r+ and various fconfigure/filevenets and none seem to work.
>
| AI bot solutions end up hanging on the reading part as well.
>
| Is this use case possible?

Why not write a temp file with the simple commands and call python with
that?

  set fd [file tempfile tmpfile]
  puts $fd $simple_commands
  close $fd
  set result [exec python $tmpfile]
  
R'
Re: Python
#49
Author: saito
Date: Wed, 14 Oct 2026 19:44
26 lines
2127 bytes
On 10/14/2026 10:30 AM, Arjen wrote:
> 
> If it does not work, can you show us via a simple/minimal example
> how you are trying to do it?

I am getting back to it. I read thru the docs on [chan pipe]. It seems a 
powerful and flexible construct. Unfortunately no detailed explanation 
or examples exist beyond redirecting or controlling the output part.


This has syntax errors:

=======================================
lassign [chan pipe] toPy fromPy

chan configure $toPy -buffering line
chan configure $fromPy -buffering line

set fileID [open "|python.exe <$toPy 2>@ $fromPy" r]
=======================================

% couldn't read file "file52551d0": no such file or directory

where that handle is the value of "toPy"

Re: Python
#51
Author: saito
Date: Wed, 14 Oct 2026 20:08
21 lines
1683 bytes
On 10/14/2026 12:04 PM, Ralf Fassel wrote:
> 
> Why not write a temp file with the simple commands and call python with
> that?
> 
>    set fd [file tempfile tmpfile]
>    puts $fd $simple_commands
>    close $fd
>    set result [exec python $tmpfile]
>    

Commands are read at a time from a text widget when a button is clicked. 
So this would require idle file reads and waits at both Tcl and python, 
if I am not mistaken.






Re: Python
#53
Author: Rich
Date: Thu, 15 Oct 2026 02:45
38 lines
2573 bytes
saito <saitology9@gmail.com> wrote:
> On 10/14/2026 10:30 AM, Arjen wrote:
>> 
>> If it does not work, can you show us via a simple/minimal example
>> how you are trying to do it?
> 
> I am getting back to it. I read thru the docs on [chan pipe]. It seems a 
> powerful and flexible construct. Unfortunately no detailed explanation 
> or examples exist beyond redirecting or controlling the output part.
> 
> 
> This has syntax errors:
> 
> =======================================
> lassign [chan pipe] toPy fromPy
> 
> chan configure $toPy -buffering line
> chan configure $fromPy -buffering line
> 
> set fileID [open "|python.exe <$toPy 2>@ $fromPy" r]
> =======================================
> 
> % couldn't read file "file52551d0": no such file or directory
> 
> where that handle is the value of "toPy"

From the 'exec' man page, '<' is defined as working with "files" (i.e., 
filenames).

chan pipe gives you file id's, not names.

You want the <@ version:

<@ fileId      FileId  must be the identifier for an open file, such as
                      the return value from a previous call to  open.   It  is
                      used  as the standard input for the first command in the
                      pipeline.  FileId must have been opened for reading.
Re: Python
#54
Author: Christian Gollwitzer
Date: Thu, 15 Oct 2026 08:37
48 lines
2572 bytes
Am 14.10.26 um 15:07 schrieb saito:
> I would like to start a python session, send it some simple commands and 
> get the results back. From Tcl, I have tried opening a pipe with RDWR 
> and r+ and various fconfigure/filevenets and none seem to work.
> 
> AI bot solutions end up hanging on the reading part as well.
> 
> Is this use case possible?

Maybe it's failing because python uses GNU readline and expects a real 
terminal, and behaves differently when you redirect stdin to a pipe.

YOu could write a Python script that reads from stdin and executes the 
commands. I'm using a scheme like this in a larger Python application 
that is remotely controlled from a Tcl GUI.

Python code is like  this:

     def slave_loop(self):
         tkroot.createfilehandler(sys.stdin, tkinter.READABLE, 
self.stdin_handler)

     def stdin_handler(self, file, mask):
         cmd = file.readline()

         if not cmd:
             exit()
         try:
             print(eval(cmd))
         except:
             print('ERROR')
             traceback.print_exc()

         self.slave_loop()



The Python program in this case is itself a Tkinter app, but it should 
work similarly well without an event loop like this

while True:
     cmd=sys.stdin.readline()
     eval(cmd)

Obviously needs some error handling etc.

Christian
Re: Python
#56
Author: saito
Date: Thu, 15 Oct 2026 12:52
70 lines
2859 bytes
On 10/15/2026 4:37 AM, Christian Gollwitzer wrote:
> 
> YOu could write a Python script that reads from stdin and executes the 
> commands. I'm using a scheme like this in a larger Python application 
> that is remotely controlled from a Tcl GUI.
> 

I would prefer the Tcl way but I can't make it work. So I ended up doing 
something similar yesterday. Through a few iterations of the idea, this 
is what it looks like now:

===== save to driver.py ====================

import sys

errCounter = 0

while True:
     try:
         line = input()
     except EOFError:
         break

     try:
         try:
             # simple expression?
             result = eval(line)
             if result is not None:
                 print(result)

         except SyntaxError:
             # code block?
             exec(line)

     except Exception as e:
         errCounter=errCounter+1
         print(f"ERROR #{errCounter}: ", e)

============================================


This is how to use it:

============================================
set fid [open |[list python.exe -u driver.py 2>@1] r+]

fconfigure $fid -buffering line -blocking 0

puts $dif {X = 10}
gets $fid
puts $fid {X}
gets $fid

============================================


It works quite well. Things to note:

1) Currently both input and output is line oriented.

2) If the result from python has multiple lines, you need to go multiple 
gets to consume all.

Both of these could be improved upon but it seems to work quite well at 
the moment for the task it was meant for.




Re: Python
#57
Author: Ralf Fassel
Date: Thu, 15 Oct 2026 16:31
33 lines
2108 bytes
* saito <saitology9@gmail.com>
| It works quite well. Things to note:
>
| 1) Currently both input and output is line oriented.
>
| 2) If the result from python has multiple lines, you need to go
|    multiple gets to consume all.

I have similar inter-process-pipes, though between two TCL processes.

For line-oriented communication, I simply base64-encode the whole
content and send one line, base64-decode on the other side.
That way each message (regardless how long within sane limits :-)
is one line only.

Sender:
  package require base64
  proc send_base64 {chan txt} {
    set line [base64::encode -maxlen 0 [encoding convertto utf-8 $txt]]
    # assumes line-buffering
    puts $chan $line
  }

Receiver:
  package require base64
  proc recv_base64 {chan} {
      # error and eof handling omitted
      gets $chan line
      return [encoding convertfrom utf-8 [base64::decode $line]]
  }

HTH
R'
Re: Python
#58
Author: saito
Date: Thu, 15 Oct 2026 17:04
12 lines
1679 bytes
On 10/15/2026 12:31 PM, Ralf Fassel wrote:
> 
> I have similar inter-process-pipes, though between two TCL processes.
> 
> For line-oriented communication, I simply base64-encode the whole
> content and send one line, base64-decode on the other side.
> That way each message (regardless how long within sane limits :-)
> is one line only.
> 

Awesome! This will work well.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads