in reply to capturing output of system call inside a thread

It does not surprise me that mixing Capture::Tiny with threading doesn't work well. Indeed, it would surprise me if it did. The standard file handles are process-global entities and thus shared by all the threads in the process.

If you want your threads to all write to them concurrently, then you would need to use locking to ensure that the output from different threads didn't become mixed up and corrupted. That's relatively trivial to do:

use threads::shared; my $sem : shared; sub tprint { lock $sem; print @_; } sub twarn { lock $sem; warn @_; }

But attempting to add Capture::Tiny into the mix simply isn't going to work as it uses fork to perform its work, and is completely oblivious to threading.

As for an alternative; I cannot work out from your question or your toy code what it is that you're actually trying to achieve.

But, if you can describe what it is that you need to do; rather than how you are currently trying to do "it"; then I might be able to help.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.
I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!

Replies are listed 'Best First'.
Re^2: capturing output of system call inside a thread
by that_guy (Novice) on Sep 03, 2015 at 18:22 UTC
    Thanks for the reply. I want to have multiple concurrently running "processes". These "processes" are independent of each other. Each "process" can call system, print to STDOUT and STDERR, but I want outputs (both STDOUT and STDERR) and any output from system() of each of these "processes" to be captured in separate files preferably with STDOUT and STDERR outputs arranged properly. This way I would have meaningful log files for each process. Does that make sense?
      I want outputs (both STDOUT and STDERR) and any output from system() of each of these "processes" to be captured in separate files preferably with STDOUT and STDERR outputs arranged properly.

      What do you mean by:

      1. "any output from system()"?

        system doesn't produce output.

        Programs run using system produce output; but you've already said you want to capture that.

        So what other output ("produced by system()") are you expecting?

      2. "with STDOUT and STDERR outputs arranged properly"?

        stdout is usually buffered; whereas stderr is usually unbuffered; which means that stuff written to stderr often appears on the console before stuff that was written to stdout chronologically earlier in the execution of the program.

        There is nothing that can be done from outside of the program to influence the that.

        If you have access to the sources of the external programs you are running; you can ensure that both streams are unbuffered; but that has nothing to do with threading, or the perl code that is invoking those programs.

      I think I can suggest something to achieve your goals; but I need you to answer those two questions first.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
      I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
        Thanks for the reply again.

        1. Sorry I was imprecise. I've meant that I want to capture output of the program that is being run by system call: STDOUT and STDERR, as well as STDOUT and STDERR produced by perl code in that "process".

        2. You are correct is saying I can't control external program buffering from my perl script. I just want to see in my log file the same thing that I would see on the screen if I were to run that program independently from the command line.