Reputation: 722
I have a tcl proc called run_expect that I use to run basic tcl expect flow: spawn <device>
, send <cmd>
, expect <string>
. Now I need to run this code from 2 threads running in parallel, I did the following attemps:
run_expect
I got the error of unknown command run_expect
from the thread's context/scope.run_expect
proc and put it in the thread itself, but then I encountered another issue that the thread doesn't seem to see expect library as the other procs and complains on: "invalid command name "spawn"
.package require Expect
from the thread itself, but got Segmentation fault: 11
error.::audio_path
variable of the thread to be same as main context but also didn't help making the package require work (::thread::send -async [lindex $tids 0] [list set ::auto_path $::auto_path]
)Is there anyway to call any already existing proc from a thread? if not, is moving the code into the thread is the write solution? and how can I get the thread to know the packages / commands loaded?
Upvotes: 1
Views: 194
Reputation: 137587
Each thread in Tcl is almost totally isolated from all other threads. They don't share any commands (including procedures) or variables. The easiest way to manage things in multiple threads is to put the code to be in each thread into its own Tcl file and to tell the worker threads to source
that as part of starting up.
The Expect package is not thread safe; you've provided clear evidence of that. I don't know the details of why this is so. This means that if you want multithreaded expect
ing, your easiest approach is to use several processes instead. Tcl's good at managing subprocesses via asynchronous pipelines, and when everything is designed to work that way, you don't need to use Expect in the parent to manage things. You could also use the comm package to do the communications with the subprocesses.
Upvotes: 1