0
votes

I am trying to search information if inter process communication can happen with tcl threads. I am biginner on this topic so right now just collecting information. I understand that sender and receiver mechanism to be coded to pass data between processess. And tcl thread package provides send command. Also thread can be used as timer for spawn process inside the same. Is it possible to recieve data from thread to another thread? Thanking you.

#contains of test.tcl
puts stdout "hello from wish"
# end of file

# set cmd
set exe {wish85.exe}

set exepath [list $exe test.tcl]

# This next line is slightly magical
set f [open |$exepath r+]
# Use the next line or you'll regret it!
puts $f {fconfigure stdout -buffering line}

fileevent $f readable "getline $f"
proc getline f {

       if {[gets $f line]<0} {

           close $f ;
           return ;
        }
puts "line=$line"
} 
1

1 Answers

2
votes

You need to be much clearer in your mind about what you are looking for. Threads are not processes! With Tcl, every Tcl interpreter context (the thing you make commands and variables in) is bound to a single thread, and every thread is coupled to a single process.

Tcl has a Thread package for managing threads (it should be shipped with any proper distribution of Tcl 8.6) and that provides a mechanism for sending messages between threads, thread::send. Those messages? They're executable scripts, which means that they are really flexible.

For communication between processes, things are much more complicated because you have to consider both discovery of the other processes and security (because processes are a security boundary by design). Here are some of the options:

  • Tcl is very good at running subprocesses and talking with them via pipes. For example, you can run a subordinate interpreter in just a couple of lines using open:

    # This next line is slightly magical
    set mypipeline [open |[list [info nameofexecutable]] r+]
    
    # Use the next line or you'll regret it!
    puts $mypipeline {fconfigure stdout -buffering line}
    

    It even works well with the fileevent command, so you can do asynchronous processing within each interpreter. (That's really quite uncommon in language runtimes, alas.)

  • The send command in Tk lets you send scripts to other processes using the same display (I'm not sure if this works on Windows) much as thread::send does with threads in the same process.

  • The comm package in Tcllib does something very similar, but uses general sockets as a communication fabric.

  • On Windows, you can use the dde command in Tcl to communicate with other processes. I don't think Tcl registers a DDE server by default, but it's pretty easy to do (provided you are running the event loop, but that's a common requirement for most of the IPC mechanisms to work at their best).

More generally, you can think in terms of running webservices and so on, but that's getting quite complicated!