* meshparts <alexandru.dadalau@meshparts.de>Okay, got it!
| I'm using the code below to start a process and wait for the process
| to finish and get the return value.
| the script test4.tcl only contains a "puts" for the result and and
| "exit" so tht the process is ended.
| It works, but the result is written to the given output file.
| Instead I would like it to behave more like a return value, to get the
| result of the script directly.
The canonical way would be to set up a pipe via [chan pipe]
https://www.tcl-lang.org/man/tcl/TclCmd/chan.html#M30
https://wiki.tcl-lang.org/page/chan+pipe
and pass the write-end as stdout to the process and read the output via
the read-end in your process.
HTH
R'
* Ralf Fassel <ralfixx@gmx.de>At least with Tcl 8 I hat issues with using exec with paths containing
| * Ralf Fassel <ralfixx@gmx.de>
| | If you want to send to the process as well, you need a second pipe:
| >
| | lassign [chan pipe] processStdin writeChanId
| | lassign [chan pipe] readChanId processStdoutErr
| >
| | ::twapi::create_process ... \
| | -stdchannels [list $processStdin $processStdoutErr $processStdoutErr]
| >
| | Now whatever you write to writeChanId arrives in the process at
| | processStdin, and whatever the process writes to processStdoutErr,
| | arrives at your readChanId.
| But I wonder why you don't use the regular TCL [open "|"] syntax, where
| you get all this for free?
| set fd [open "| process" w+]
Or even plain 'exec', since you wait for the process anyway?
set result [exec process]
R'
Ah, no. This would connect the stdout of the process to its own stdin.
If you want to send to the process as well, you need a second pipe:
lassign [chan pipe] processStdin writeChanId
lassign [chan pipe] readChanId processStdoutErr
::twapi::create_process ... \
-stdchannels [list $processStdin $processStdoutErr $processStdoutErr]
Now whatever you write to writeChanId arrives in the process at
processStdin, and whatever the process writes to processStdoutErr,
arrives at your readChanId.
R'
Hm, I can't get it work this way. Either I get nothing from the "gets" command or it complains that the channel was not open for reading.
I understand your point, but somehow my solution worked...
You need to take the usual configuration of channels into account:
- make sure to flush your output channel (or set it unbuffered) to make
sure any output is actually sent to the process
- you might need to close the 'unused' ends of the pipe in your process
after creating the child process (seehttps://www.tcl-lang.org/man/tcl/TclCmd/chan.html#M30)
to get correct [eof] notifications
- make sure to avoid deadlocks if you send more data than the process
reads by setting the pipe channels non-blocking
R'
are you sureHm, I was sure, because I'm closing after the wait comman (vwait ::meshparts::twapi_pid($pid)) which in turn only finishes after the
::GeneratePartBatchRead has collected all data before you call 'close'
I'd rather let ::GeneratePartBatchRead decide when tobesides, how can it be, that the first message is never received while
stop: if it gets eof on the readChanId, the other side should have
closed it (note that you will need to close that channel in your
process, too, see chan manpage on 'pipe').
| Sysop: | DaiTengu |
|---|---|
| Location: | Appleton, WI |
| Users: | 1,089 |
| Nodes: | 10 (0 / 10) |
| Uptime: | 153:53:24 |
| Calls: | 13,921 |
| Calls today: | 2 |
| Files: | 187,021 |
| D/L today: |
3,760 files (944M bytes) |
| Messages: | 2,457,163 |