codingJoe
codingJoe

Reputation: 4953

Python threading question

I have some python application with 2 threads. Each thread operates within a separate gui. The GUIs need to operate independently without blocking. I am trying to figure out how to make thread_1 trigger an event to happen in thread_2?

Below is some code I want function foo to trigger function bar in the simplest, most elegant way as quickly as possible, without consuming unnecessary resources. Below is what I've come up with.

bar_trigger=False  #global trigger for function bar.
lock = threading.Lock()

class Thread_2(threading.Thread):
  def run(self):
    global lock, bar_trigger
    while(True):
       lock.acquire()
       if bar_trigger==True:
         Thread_2.bar()  #function I want to happen 
         bar_trigger=False
       lock.release()
       time.sleep(100) #sleep to preserve resources
                       #would like to preserve as much resources as possible 
                       # and sleep as little as possible.

  def bar(self):
       print "Bar!"

class Thread_1(threading.Thread):
  def foo(self):
      global lock, bar_trigger
       lock.acquire()
       bar_trigger=True  #trigger for bar in thread2
       lock.release()

Is there a better way to accomplish this? I'm not a threadding expert so any advice on how to best trigger a method in thread_2 from within thread_1 is appreciated.

Upvotes: 0

Views: 219

Answers (3)

Mahmoud Abdelkader
Mahmoud Abdelkader

Reputation: 24999

Without knowing what you're doing and what GUI framework you're using, I can't get into much more detail, but from your problem's code snippet, it sounds like you're looking for something called conditional variables.

Python comes with them included by default in the threading module, under threading.Condition You might be interested in threading.Event as well.

Upvotes: 1

Jon Cage
Jon Cage

Reputation: 37518

One option would be to share a queue between the threads. Thread 1 would push an instruction into the queue and thread two would poll that queue. When Thread 2 sees the queue is non-empty, it reads off the first instruction in the queue and calls the appropriate function. This has the additional benefit of being fairly loosely couple which can make testing each thread in isolation easier.

Upvotes: 0

Santa
Santa

Reputation: 11545

How are these threads instantiated? There should really be a main thread that oversees the workers. For example,

import time
import threading

class Worker(threading.Thread):
    def __init__(self, stopper):
        threading.Thread.__init__(self)
        self.stopper = stopper

    def run(self):
        while not self.stopper.is_set():
            print 'Hello from Worker!'
            time.sleep(1)

stop = threading.Event()
worker = Worker(stop)
worker.start()

# ...

stop.set()

Using a shared Event object is just one way of synchronizing and sending messages between threads. There are others, and their usages depend on the specifics.

Upvotes: 0

Related Questions