stopping LoopCall and check if running

Stephan <[email protected]>
Newsgroups gmane.comp.python.twisted.web
Message-ID <CADozHMU59U9BTot3ijD0Ks1N2PM02T-UYb8NuLSyQpH7kzpAKw@mail.gmail.com>
I have a question on LoopCall.

I have a process that uses a sequence of LoopCalls, once one finishes
I call the next to check for the next critical event to continue:

e.g.

    def checkForAvailableProfile(self):
        profile = fs_profiles_manager.get_next_available_profile()
        if profile is None:
            return
        else:
            self.checkForAvailableProfileLoop.stop()

        self.profile = profile

        logging.debug("using profile: %s", self.profile['name'])

        self.ff = FirefoxProcess(self.profile, self.params)
        self.ff.run()

        self.checkForEventsFileLoop = LoopingCall(self.checkIfEventsFileExists)
        self.checkForEventsFileLoop.start(0.5)
        self.state = self.STATE_WAIT_FOR_EVENTS

====================================================

The problem is that I'm getting errors, errors when I try to call stop
when stop perhaps has already been called.
I want advice on how to manage LoopCalls. So far I'm using state variables.
e.g.
self.state = self.STATE_WAIT_FOR_EVENTS

if something goes haywire or if the process is done I have a function
that cleans up all the LoopCalls so it's not checking constantly:

    def cleanExit(self):
        if self.state == self.STATE_WAIT_FOR_PROFILE:
            self.checkForAvailableProfileLoop.stop()
        elif self.state == self.STATE_WAIT_FOR_EVENTS:
            self.checkForEventsFileLoop.stop()
        elif self.state == self.STATE_WAIT_FOR_PROC_KILLED:
            self.checkIfFirefoxWasKilledLoop.stop()

        self.ff.cleanExit()


the above code is what I believe is causing my exceptions, but how
else do I check for the LoopCalls?



=====================================================

2011-08-31 12:30:11-0700 [FiresharkProtocol,1278,127.0.0.1] Unhandled Error
    Traceback (most recent call last):
      File "/usr/lib/python2.6/dist-packages/twisted/application/app.py",
line 348, in runReactorWithLogging
        reactor.run()
      File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py",
line 1170, in run
        self.mainLoop()
      File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py",
line 1182, in mainLoop
        self.doIteration(t)
      File "/usr/lib/python2.6/dist-packages/twisted/internet/selectreactor.py",
line 140, in doSelect
        _logrun(selectable, _drdw, selectable, method, dict)
    --- <exception caught here> ---
      File "/usr/lib/python2.6/dist-packages/twisted/python/log.py",
line 84, in callWithLogger
        return callWithContext({"system": lp}, func, *args, **kw)
      File "/usr/lib/python2.6/dist-packages/twisted/python/log.py",
line 69, in callWithContext
        return context.call({ILogContext: newCtx}, func, *args, **kw)
      File "/usr/lib/python2.6/dist-packages/twisted/python/context.py",
line 59, in callWithContext
        return self.currentContext().callWithContext(ctx, func, *args, **kw)
      File "/usr/lib/python2.6/dist-packages/twisted/python/context.py",
line 37, in callWithContext
        return func(*args,**kw)
      File "/usr/lib/python2.6/dist-packages/twisted/internet/selectreactor.py",
line 156, in _doReadOrWrite
        self._disconnectSelectable(selectable, why, method=="doRead")
      File "/usr/lib/python2.6/dist-packages/twisted/internet/posixbase.py",
line 194, in _disconnectSelectable
        selectable.connectionLost(f)
      File "/usr/lib/python2.6/dist-packages/twisted/internet/tcp.py",
line 519, in connectionLost
        protocol.connectionLost(reason)
      File "/usr/sbin/fireshark.py", line 101, in connectionLost
        self.cleanExit()
      File "/usr/sbin/fireshark.py", line 43, in cleanExit
        self.checkIfFirefoxWasKilledLoop.stop()
      File "/usr/lib/python2.6/dist-packages/twisted/internet/task.py",
line 171, in stop
        assert self.running, ("Tried to stop a LoopingCall that was "
    exceptions.AssertionError: Tried to stop a LoopingCall that was not running.

2011-08-31 12:30:22-0700 [-] Unhandled error in Deferred:
2011-08-31 12:30:22-0700 [-] Unhandled Error
    Traceback (most recent call last):
      File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py",
line 1170, in run
        self.mainLoop()
      File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py",
line 1179, in mainLoop
        self.runUntilCurrent()
      File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py",
line 778, in runUntilCurrent
        call.func(*call.args, **call.kw)
      File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py",
line 944, in unwindGenerator
        return _inlineCallbacks(None, f(*args, **kwargs), Deferred())
    --- <exception caught here> ---
      File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py",
line 823, in _inlineCallbacks
3, in _inlineCallbacks
        result = g.send(result)
    exceptions.AttributeError: 'NoneType' object has no attribute 'send'

2011-08-31 12:30:28-0700 [-] Unhandled error in Deferred:
2011-08-31 12:30:28-0700 [-] Unhandled Error
    Traceback (most recent call last):
      File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py",
line 1170, in run
        self.mainLoop()
      File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py",
line 1179, in mainLoop
        self.runUntilCurrent()
      File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py",
line 778, in runUntilCurrent
        call.func(*call.args, **call.kw)
      File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py",
line 944, in unwindGenerator
        return _inlineCallbacks(None, f(*args, **kwargs), Deferred())
    --- <exception caught here> ---
      File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py",
line 823, in _inlineCallbacks
        result = g.send(result)
    exceptions.AttributeError: 'NoneType' object has no attribute 'send'
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.