Re: Thread Question



Em Ter, 2006-02-28 às 20:24 +0000, Grant Edwards escreveu:
I have seen examples that used classes, and other examples
that just called one thread start command - when should you
use one over another?

I'm not sure what you mean by "use classes" vs. "calling a
thread start command". My example above uses a class
(threading.Thread) to create a thread object, and then calls
its start method.

# He meant calling direct vs. subclassing. In your example you called
the Thread class directly, but you could have subclassed it.

# In your case, Edwards, I'd prefer subclassing because then you could
put some states in the class. A (bad) example:

class Foo(Thread):
def __init__(self):
Thread.__init__(self)
self.alive = False
self.running = True

def run(self):
while self.running:
self.alive = ping('myhost')
sleep(10)

def stop(self):
self.running = False

# Then you could:

a = Foo()
do_something()
print a.alive
do_something_more()
print a.alive
finish_everything()
print a.alive
a.stop()
# quit

--
"Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação'. Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas."

-- Sun Tzu, em "A arte da guerra"

.



Relevant Pages

  • Re: Clearing the screen
    ... This command works when using python in terminal mode, ... outros povos sem travar batalha, ...
    (comp.lang.python)
  • Re: Call statements
    ... I have a fair amount of repetitive code, sometimes exactly what a command ... the code into that for reuse instead of calling the button. ... The only real "danger" in invoking controls in this manner, ... not only is it "best to separate Presentation ...
    (microsoft.public.vb.general.discussion)
  • Re: Problems call DataAdapter.Update and Concurrency
    ... I use a command builder for update command as I want to make use of the ... as soon as the data gets changed it causes a concurrency violation. ... calling the .fill and avoiding another trip to the database. ... By default the DataAdapter will call AcceptChanges on each succesfull ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Adept: Discwasher calling-four
    ... Instead, you insert Discwasher's Calling Four card into slot number seven of the Apple's main board, which is no problem at all if your Apple is accompanied by its manual. ... A connector from the expansion card then plugs into the spot where the joy stick, say, would normally plug into the computer, and the joy stick plug goes into the new card, along with those of any three other controllers you might want to use. ... True, only a single command is involved, but there are preliminaries to be attended to, and here the manual accompanying Calling Four begins to let the novice down. ...
    (comp.sys.apple2)
  • Re: Lets leave this confusion and all this illusion behind in the world
    ... has directly commanded you to bless those who curse you. ... sons of vipers is just as bad as calling them dimwits or dopes. ... You aren't Jesus. ... There is no command not to call anyone names. ...
    (soc.religion.quaker)