On 6/27/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
Tino Dai wrote:
> And there is one caveat, I
> will have to make a bunch of semaphores global instead of local to the
> classes. While I know that there is no hard and fast rule about using
> global variables, where can I find or can somebody tell me where I can
> find some guidelines about them (or is this a use common sense rule)?

Main guideline - don't use globals. Fallback rule - don't use globals. Third rule - OK, if you really can't think of any other way, make it a global. :-)

Can you pass the semaphore to the class constructor (__init__() method)? What is it about the unit test that pushes you to make it global?

Man, spoil all my fun :) What I'm doing is I have a set.......actually...hang on...are you talking about the program or the unit test? The unit test doesn't have an globals. The program itself does. What the program is doing launching a bunch of threads that are linked together via queues. The reading of the queues by the next stage in the program is controlled by a semaphore. The semaphore will release on one side and acquire on the other side. The data is passed along the different threads until the data is indexed. The semaphores are global so that the unit test can bring in only one class at a time. How I had it before was: the semaphore would be local to the class and subsequent class would all that local semaphore. I think it might be easier if I just shown you.

How I had it before:

class nameA:
    sema = threading.semaphore()
    def __init__(self):
        <do some stuff>

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

class nameB:
     def __init__(self):
        <do some stuff>
 
     def run(self):
         nameA.sema.acquire()
         <do some stuff>


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?

-Tino




_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to