[Tutor] Unit testing

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Wed Jun 28 20:24:50 CEST 2006


Tino Dai schreef:
> How I have it now:
> 
> semaA = threading.semaphore()
> 
> class nameA:
>    def __init__(self):
>         <do some stuff>
>  
>    def run(self):
>          <do some stuff>
>          semaA.release()
>         
> class nameB:
>    def __init__(self):
>         <do some stuff>
>  
>    def run(self):
>          semaA.acquire()
>          <do some stuff>
>         
> 
> Does that make sense. Or is there a better way?

I think it's better to do something like:

class nameA:
     def __init__(self, sema):
         self.sema = sema
         <other stuff>

     def run(self):
         <do some stuff>
         self.sema.release()

class nameB:
     def __init__(self, sema):
         self.sema = sema
         <other stuff>

     def run(self):
         self.sema.acquire()
         <do some stuff>

Then where you create instances of those classes:

sema = threading.semaphore()
a = nameA(sema)
b = nameB(sema)


Maybe you don't even need the semaphore at all: have a look at 
Queue.Queue, it might do exactly what you need.

-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

Roel Schroeven



More information about the Tutor mailing list