Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92021

Re: Multiple thread program problem

Newsgroups comp.lang.python
Date 2015-06-03 19:59 -0700
References <122a6965-5fe9-42bf-ba6d-393288121905@googlegroups.com> <mailman.139.1433378292.13271.python-list@python.org>
Message-ID <bdd7aa19-1dca-4fc4-a998-c6fd6997375c@googlegroups.com> (permalink)
Subject Re: Multiple thread program problem
From M2 <mohan.mohta@gmail.com>

Show all headers | View raw


On Wednesday, June 3, 2015 at 7:38:22 PM UTC-5, Cameron Simpson wrote:
> On 03Jun2015 17:04, M2 <mohan.mohta@gmail.com> wrote:
> >On Wednesday, June 3, 2015 at 6:56:47 PM UTC-5, sohca...@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@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@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Multiple thread program problem Mohan Mohta <mohan.mohta@gmail.com> - 2015-06-03 13:41 -0700
  Multiple thread program problem Sam Raker <sam.raker@gmail.com> - 2015-06-03 14:01 -0700
    Re: Multiple thread program problem Mohan Mohta <mohan.mohta@gmail.com> - 2015-06-03 15:23 -0700
      Re: Multiple thread program problem Mohan Mohta <mohan.mohta@gmail.com> - 2015-06-03 16:45 -0700
        Re: Multiple thread program problem sohcahtoa82@gmail.com - 2015-06-03 16:56 -0700
          Re: Multiple thread program problem M2 <mohan.mohta@gmail.com> - 2015-06-03 17:04 -0700
            Re: Multiple thread program problem Cameron Simpson <cs@zip.com.au> - 2015-06-04 10:37 +1000
              Re: Multiple thread program problem M2 <mohan.mohta@gmail.com> - 2015-06-03 19:59 -0700
                Re: Multiple thread program problem Cameron Simpson <cs@zip.com.au> - 2015-06-04 14:42 +1000
  Re: Multiple thread program problem MRAB <python@mrabarnett.plus.com> - 2015-06-03 21:59 +0100
  Re: Multiple thread program problem Gary Herron <gherron@digipen.edu> - 2015-06-03 15:02 -0700
  Re: Multiple thread program problem M2 <mohan.mohta@gmail.com> - 2015-06-04 10:20 -0700
    Re: Multiple thread program problem Cameron Simpson <cs@zip.com.au> - 2015-06-05 08:58 +1000

csiph-web