http://www.perlmonks.org?node_id=1052627


in reply to Re^3: Get the process id, and output of a process, in a threaded environment
in thread Get the process id, and output of a process, in a threaded environment

That would be a way to go about it. Unfortunately however, I am using a thread pool (as you and I have previously decided would be best solution for the application described in this thread, which has undergone a number of improvements but overall structure is the same), so any option of setting the environment before thread creation is out of the question, as it will need to constantly change on a per command basis.

I'll update my original post to have a more clear description of my script

  • Comment on Re^4: Get the process id, and output of a process, in a threaded environment

Replies are listed 'Best First'.
Re^5: Get the process id, and output of a process, in a threaded environment
by BrowserUk (Patriarch) on Sep 06, 2013 at 11:28 UTC

    I've thought about this overnight and I don't have any good news for you.

    If *nix doesn't allow you to control the environment that a child process gets when it is spawned from a thread, that's not the fault of nor can it be influenced or corrected by anything you do in your Perl code.

    One alternative I can come up with is that you write your own 'shell' that in addition to doing the require SETs and forking the command, somehow conveys the pid of the forked process back to its parent (your Perl script).

    Maybe, it could arrange for the child's pid to be returned as the first line that is read from the pipe?

    Another alternative (that I'm barely aware of what might be possible), is to use the memory mapped file structure (is it /dev/...?) to discover the child's pid. I'll leave that idea hanging their cos I don't know enough to know if it is possible or portable.

    If you arrive at a solution, please be sure to come back and tell us how you did it, because I can see that it is something others would find useful.


    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".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Darn, I was hoping you would have better news!

      The only solution that I could come up with was basically your first solution, and would basically write a perl script and open that with a pipe. This script would do the required SETS, open the process that we can now get the actual pid from, and print the pid as the first line, something along the lines of PID=<pid>, which is just what you described.

      Far from an elegant solution, but it was the only one I saw.

      As for your second solution, I'm with you not having idea of if its possible, and something tells me getting it to work on all *nix environments AND windows, would be a nightmare.

      As always, thanks for taking a look!