nukescripts.pyAppUtils

Package nukescripts :: Module pyAppUtils |
[hide private]
[frames] | no frames]

# Source Code for Module nukescripts.pyAppUtils

     1  # Copyright (c) 2009 The Foundry Visionmongers Ltd.  All Rights Reserved.
     2
     3  import Queue
     4  import sys
     5  import threading
     6  import nuke
     7
     8
     9 -class pyAppHelper(object):
    10    """ Helper class to run python commands in a separate thread. """
    11
    12 -  def __init__(self, start = None):
    13      """ constructor """
    14      self.__pyThread = threading.Thread(target = self.__pyThreadMain)
    15      self.__pyThread.setDaemon(True)
    16      self.__working = False
    17      self.__work = Queue.Queue()
    18
    19
    20 -  def run(self, call, args = (), kwargs = {}):
    21      """ Runs the specified call in a separate thread. """
    22      self.__work.put((call, args, kwargs), True)
    23
    24
    25 -  def initiate(self):
    26      """ Start the thread associated with this object """
    27      if not self.__pyThread.isAlive():
    28        self.__working = True
    29        self.__pyThread.start()
    30
    31
    32 -  def terminate(self):
    33      """ Terminated the thread associated with this object """
    34      if self.__working:
    35        self.__working = False
    36        self.__pyThread.join()
    37        while not self.__work.empty():
    38          self.__work.get_nowait()
    39
    40
    41 -  def __pyThreadMain(self):
    42      """ Thread entry function """
    43      while self.__working:
    44        try:
    45          func, args, kwargs = self.__work.get(True)
    46          func(*args, **kwargs)
    47        except Exception:
    48          sys.excepthook(*sys.exc_info())
    49