Multiple thread program problem
M2
mohan.mohta at gmail.com
Wed Jun 3 22:59:23 EDT 2015
On Wednesday, June 3, 2015 at 7:38:22 PM UTC-5, Cameron Simpson wrote:
> On 03Jun2015 17:04, M2 <mohan.mohta at gmail.com> wrote:
> >On Wednesday, June 3, 2015 at 6:56:47 PM UTC-5, sohca... at gmail.com wrote:
> >> On Wednesday, June 3, 2015 at 4:45:52 PM UTC-7, M2 wrote:
> >> > On Wednesday, June 3, 2015 at 5:34:31 PM UTC-5, Waffle wrote:
> >> > > You think "(f)" makes a tuple, but it does not.
> >> > > the parentesis is not the tuple constructor, the comma is
> >> > > try:
> >> > > t=thread.start_new_thread(proc,(f,))
> >> >
> >> > Thanks for the pointer waffle.
> >> > The program executes now but still not the way I want it.
> >> > I think I will need to tweak it a bit as the code is executing with the same argument from the file /tmp/python/1 multiple times whereas it needs to be executed only ones but in parallel. Let me figure that out.
> >> >
> >> >
> >> > Once again thanks for all the help provided on this thread.
> >>
> >> Check your usages of "line" and "f". You have spots where you probably meant "line" instead of "f", and others where you have "f" where you probably meant "line".
> >
> >Here is my logic:
> >f is where the entire file is getting loaded
>
> In the main code, yes.
>
> >which is also passed as argument in the function proc
>
> But why? f is not using in proc. Only line is.
>
> >line has a single line from the file which is then stripped off the new line character and assigned to com2 variable which helps in using it in the subprocess.call
>
> That end is fine.
>
> I would be passing only "line" to proc, not "f" at all.
>
> Suggestion: move your main code into its own function. That will make all the
> variables in it "local". Your proc function is presently relying on "line"
> being global, which generally bad and a recipe for disaster in multithreaded
> code.
>
> Moving the main code into its own function will (1) get rid of the global
> variables and (2) force you to consider exactly what you need to pass to
> "proc", and that will help reveal various logic issues.
>
> Cheers,
> Cameron Simpson <cs at zip.com.au>
>
> >>>How do you blip the throttle and wave? Do you blip it real high, then wave
> >>>before the revs drop back?
> >>Blip = right hand; Wave = left hand. Do both simultaneously. QED.
> >Doesnt this make the bike lurch forward thru the intersection?
> Not if the disk lock is in place...
> - Dean Woodward <deanw at agora.rdrop.com>
Thanks Cameron.
I do not see the duplication in the execution now.
I do see it is not consistent by executing all the threads ; it might be due to the fact I am using
subprocess.call(co,shell=True)
Per my understanding the above does not keep track of threads it just spawns a thread and leaves it there.
I might need to use the function start(), join() to ensure it picks up all the argument
For the record now my new code is
#! /usr/bin/python
import os
import subprocess
import thread
import threading
import sys
from thread import start_new_thread
def proc(col) :
subprocess.call(col,shell=True)
return
f = open('/tmp/python/1')
for line in f:
com1="ssh -B "
com2=line.strip('\n')
com3= " uname -a "
co=str("ssh -B ")+ str(com2) + str(" uname -a")
t=thread.start_new_thread(proc,(co,))
f.close()
Thanks again for the help
More information about the Python-list
mailing list