techman2006 has asked for the wisdom of the Perl Monks concerning the following question:
I need to make a thread to wait till the command run on shell get completed.
The use case is given below
Now above operations will be performed by a set of threads. So the problem is that thread don't till the execution over the shell get completed and they are overloading the system.
So is there a way I can make thread to wait till the execution over the shell get completed.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to make a thread to wait till a command on shell get completed.
by BrowserUk (Patriarch) on Dec 02, 2013 at 07:02 UTC | |
If you run a command (shell or otherwise) using system, the program will block until system returns. If the program calling system is a single threaded program, that single thread will block. If the program is a multi-threaded program, whichever thread calls system will block. Now what is your problem? 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.
| [reply] |
by techman2006 (Beadle) on Dec 02, 2013 at 07:44 UTC | |
Basically I am using back tick operator due to which multiple instance of gzip get triggered as the thread is not waiting for the shell to return. So how I can achieve blocking of thread in case of back tick. | [reply] |
by choroba (Cardinal) on Dec 02, 2013 at 08:02 UTC | |
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
| [reply] [d/l] |
by BrowserUk (Patriarch) on Dec 02, 2013 at 08:38 UTC | |
Basically I am using back tick operator due to which multiple instance of gzip get triggered as the thread is not waiting for the shell to return. Backticks also block. (Unless you are deliberately backgrounding the command, in which case, don't do that!) Post your failing code and you'll get 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".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] |
by dasgar (Priest) on Dec 02, 2013 at 08:03 UTC | |
Can you share example code that recreates the issue that you're seeing? Also, do you have a need for using back ticks? In the mean time, without seeing your code, I can think of two suggestions. First, use the system command instead of backticks like BrowserUk suggested. Second, try using modules like Archive::Tar to extract your tar files instead of using backticks. | [reply] |
|
Re: How to make a thread to wait till a command on shell get completed.
by Anonymous Monk on Dec 02, 2013 at 11:34 UTC | |
you can solve the problem by wrapping the system call inside a mutex or a semaphore. I'm afraid I don't know how to do that -- someone who knows Perl threads will have to tell you how. | [reply] [d/l] |
by techman2006 (Beadle) on Dec 02, 2013 at 15:39 UTC | |
Below is the code which I was talking about.
Now when I run above code and check how many instance of either tar or gzip is running I get below output.
Now as I have only 5 thread created I expect to have only 5 instance of tar and similarly 5 instance of gzip. But that doesn't seems to be the case. Any thoughts to fix this issue. Please note that the actual data is not text files so the archiving will take some time to complete the operation. | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on Dec 02, 2013 at 17:59 UTC | |
Now as I have only 5 thread created I expect to have only 5 instance of tar and similarly 5 instance of gzip. But that doesn't seems to be the case. You say that, and indeed you do start out with my $numThreads = 5;; but then, you immediately override that:
And then, in the loop where you create your threads you don't seem to use that variable at all:
Your has nothing to do with backticks not blocking, but is simply a programming error in that you are just starting lots and lots of threads without (it appears) any mechanism to control how many you start. 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.
| [reply] [d/l] [select] |
by techman2006 (Beadle) on Dec 02, 2013 at 18:18 UTC | |
by Anonymous Monk on Dec 03, 2013 at 08:34 UTC | |
The major change here is that it's passing the files one item at a time to the threads. You're dividing them to n buckets and passing the whole bucket to the thread at creation time. Apart from being nicer to look at, this one-at-a-time (supervisor-worker) approach should ensure that the threads will finish quicker since they're given equal amounts of work. | [reply] [d/l] |