示例#1
0
 def invokeTarget(self, target):
     """Run a target in a new RepositoryThread w/a new repository view"""
     thread = RepositoryThread(
         name=str(self.itsPath), target=target, args=(fork_item(self),)
     )
     thread.setDaemon(True)   # main thread can exit even if this one hasn't
     thread.start()
    def commitInView(self, useThread=False):
        """
        Runs a C{RepositoryView} commit. If the commit is successful calls the
        C{AbstractRepositoryViewManager._viewCommitSuccess} method otherwise calls
        the C{AbstractRepositoryViewManager._viewCommitFailed} method. Both methods
        can be subclassed to add additional functionality. An optional useThread
        argument can be passed which indicates to run the commit in a dedicated
        C{RepositoryThread} to prevent blocking the current thread.

        @param useThread: Flag to indicate whether to run the view commit in the current
                          thread or a dedicated C{RepositoryThread} to prevent blocking
        @type: boolean
        @return: C{None}
        """

        if useThread:
            thread = RepositoryThread(target=self.__commitInView)
            thread.start()

        else:
            self.__commitInView
示例#3
0
 def invokeTarget(self, target):
     """Run a target in a new RepositoryThread w/a new repository view"""
     thread = RepositoryThread(name=str(self.itsPath),
                               target=target,
                               args=(fork_item(self), ))
     thread.setDaemon(True)  # main thread can exit even if this one hasn't
     thread.start()
示例#4
0
def run_reactor(in_thread=True):
    """Safely run the Twisted reactor"""

    global _reactor_thread

    from osaf.framework.twisted import TwistedThreadPool  # initializes threads
    from twisted.python import threadpool
    threadpool.ThreadPool = TwistedThreadPool.RepositoryThreadPool

    from twisted.internet import reactor

    if not in_thread:
        if reactor.running:
            raise AssertionError("Reactor is already running")
        # enable repeated reactor runs
        def _del_pool():
            reactor.threadpool = None

        for evt in reactor.crash, reactor.disconnectAll, _del_pool:
            reactor.addSystemEventTrigger('during', 'shutdown', evt)
        reactor.run(0)
        return

    import sys

    if _reactor_thread is None:
        if threading.currentThread().getName() != "MainThread":
            raise AssertionError(
                "can't start reactor thread except from the main thread")
        limbo = [1]
        reactor.addSystemEventTrigger('before', 'startup', limbo.pop)
        _reactor_thread = RepositoryThread(name="reactor",
                                           target=run_reactor,
                                           args=(False, ))
        _reactor_thread.setDaemon(True)  # let main thread exit even if running
        _reactor_thread.start()

        while limbo and _reactor_thread.isAlive():
            pass  # wait for reactor to start or thread to die (e.g. w/error)

        if not _reactor_thread.isAlive():
            _reactor_thread = None
示例#5
0
def run_reactor(in_thread=True):
    """Safely run the Twisted reactor"""

    global _reactor_thread

    from osaf.framework.twisted import TwistedThreadPool  # initializes threads
    from twisted.python import threadpool
    threadpool.ThreadPool = TwistedThreadPool.RepositoryThreadPool

    from twisted.internet import reactor

    if not in_thread:
        if reactor.running:
            raise AssertionError("Reactor is already running")
        # enable repeated reactor runs
        def _del_pool(): reactor.threadpool = None
        for evt in reactor.crash, reactor.disconnectAll, _del_pool:
            reactor.addSystemEventTrigger('during', 'shutdown', evt)
        reactor.run(0)
        return

    import sys

    if _reactor_thread is None:
        if threading.currentThread().getName() != "MainThread":
            raise AssertionError(
                "can't start reactor thread except from the main thread"
            )
        limbo = [1]
        reactor.addSystemEventTrigger('before', 'startup', limbo.pop)
        _reactor_thread = RepositoryThread(
            name="reactor", target=run_reactor, args=(False,)
        )
        _reactor_thread.setDaemon(True) # let main thread exit even if running
        _reactor_thread.start()

        while limbo and _reactor_thread.isAlive():
            pass   # wait for reactor to start or thread to die (e.g. w/error)

        if not _reactor_thread.isAlive():
            _reactor_thread = None