示例#1
0
def execute_locked(cmd):
    '''
    Execute a shell command while holding a global lock. Ensures only a single
    thread can run a command a time.

    @param[in]  cmd             String passed to subprocess.Popen().
    @raises     thread.error    Lock could not be acquired after 30 seconds.
    @raises     Failure         Subprocess returned nonzero return code.
    '''

    for i in range(30):
        locked = execute_locked.lock.acquire(False)
        if locked:
            break
        time.sleep(1)

    if not locked:
        raise thread.error('execute_locked could not lock after 30s.')

    try:
        proc = subprocess.Popen(args=cmd, shell=True)
        rc = proc.wait()
        if rc:
            raise Failure('command return code was ' + str(rc))
    finally:
        execute_locked.lock.release()
示例#2
0
    def __init__(self, port=None, macro_executor=None, poll_interval=3):
        # listener thread will regularly try to acquire
        # this lock, when it succeeds it will quit
        self._quit_lock = threading.Lock()
        if not self._quit_lock.acquire(0):
            _log.error('cannot acquire thread quit lock !?! aborting')
            import thread
            raise thread.error("cannot acquire thread quit-lock")

        # check for data every 'poll_interval' seconds
        self._poll_interval = poll_interval
        # localhost only for somewhat better security
        self._listener_address = '127.0.0.1'
        self._port = int(port)
        self._macro_executor = macro_executor

        self._server = xmlrpc.server.SimpleXMLRPCServer(
            addr=(self._listener_address, self._port), logRequests=False)
        self._server.register_instance(self._macro_executor)
        self._server.allow_reuse_address = True

        self._thread = threading.Thread(target=self._process_RPCs,
                                        name=self.__class__.__name__)
        self._thread.setDaemon(True)
        self._thread.start()

        _log.info('scripting listener started on [%s:%s]' %
                  (self._listener_address, self._port))
        _log.info('macro executor: %s' % self._macro_executor)
        _log.info('poll interval: %s seconds', self._poll_interval)
示例#3
0
    def __init__(self, port=None, macro_executor=None, poll_interval=3):
        # listener thread will regularly try to acquire
        # this lock, when it succeeds it will quit
        self._quit_lock = threading.Lock()
        if not self._quit_lock.acquire(0):
            _log.error("cannot acquire thread quit lock !?! aborting")
            import thread

            raise thread.error("cannot acquire thread quit-lock")

            # check for data every 'poll_interval' seconds
        self._poll_interval = poll_interval
        # localhost only for somewhat better security
        self._listener_address = "127.0.0.1"
        self._port = int(port)
        self._macro_executor = macro_executor

        self._server = SimpleXMLRPCServer.SimpleXMLRPCServer(
            addr=(self._listener_address, self._port), logRequests=False
        )
        self._server.register_instance(self._macro_executor)
        self._server.allow_reuse_address = True

        self._thread = threading.Thread(target=self._process_RPCs, name=self.__class__.__name__)
        self._thread.setDaemon(True)
        self._thread.start()

        _log.info("scripting listener started on [%s:%s]" % (self._listener_address, self._port))
        _log.info("macro executor: %s" % self._macro_executor)
        _log.info("poll interval: %s seconds", self._poll_interval)
        def save():
            # ha ha ha
            try:
                raise ZeroDivisionError
            except ZeroDivisionError:
                f = sys.exc_info()[2].tb_frame.f_back

            self.stack = traceback.extract_stack(f)
            try:
                start()
            except thread.error, e:
                d = {}
                for i in threading.enumerate():
                    i = str(i)
                    d.setdefault(i, 0)
                    d[i] += 1
                print >> stream, str(d)
                raise thread.error("%s, count: %d" %
                                   (str(e).strip(), len(threading.enumerate())))
示例#5
0
        def save():
            # ha ha ha
            try:
                raise ZeroDivisionError
            except ZeroDivisionError:
                f = sys.exc_info()[2].tb_frame.f_back

            self.stack = traceback.extract_stack(f)
            try:
                start()
            except thread.error, e:
                d = {}
                for i in threading.enumerate():
                    i = str(i)
                    d.setdefault(i, 0)
                    d[i] += 1
                print >> stream, str(d)
                raise thread.error(
                    "%s, count: %d" %
                    (str(e).strip(), len(threading.enumerate())))
示例#6
0
	def release(self):
		"""
		Release a lock.

		When the lock is locked, reset it to unlocked, and return. If any other threads are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed.

		When invoked on an unlocked lock, a ``ThreadError`` is raised.

		There is no return value.
		"""

		# If we are not locked throw error
		if not self.lockActive:
			raise thread.error("release unlocked lock")

		# Remove us from the reap list
		RemoteLock.REAP_LIST.remove(self)

		# Get the number of observers in the lock queue
		numObservers = self.lockInstance.llen( "%s:observers" % self.lockKey )

		# Begin transaction ---------
		pipe = self.lockInstance.pipeline()

		# Remove us from the owners
		pipe.delete( "%s:owner" % self.lockKey)

		# Unlock observers
		for i in range(0, numObservers):
			pipe.rpush( "%s:lock" % self.lockKey, RemoteLock.INSTANCE_ID )

		# Execute transaction -------
		pipe.execute()

		# Stop the presence agent
		self.lockPresence.stop()

		# Also release the interthread lock
		self.lockActive = False
		self.lockInterThread.release()
 def fail_new_thread(*_args):
     raise thread.error()
示例#8
0
 def fail_new_thread(*args):
     raise thread.error()
示例#9
0
 def release(self):
     owner, count = self.__get()
     if owner != self.uid:
         raise thread.error('release unlocked lock')
     assert count > 0
     self.__set(count - 1)
示例#10
0
 def release(self):
     owner, count = self.__get()
     if owner != self.uid:
         raise thread.error('release unlocked lock')
     assert count > 0
     self.__set(count - 1)