Python twisted.internet.task.react() Examples

The following are 27 code examples of twisted.internet.task.react(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted.internet.task , or try the search function .
Example #1
Source File: test_task.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_synchronousStop(self):
        """
        L{task.react} handles when the reactor is stopped just before the
        returned L{Deferred} fires.
        """
        def main(reactor):
            d = defer.Deferred()
            def stop():
                reactor.stop()
                d.callback(None)
            reactor.callWhenRunning(stop)
            return d
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, [], _reactor=r)
        self.assertEqual(0, exitError.code) 
Example #2
Source File: test_task.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_runsUntilSyncErrback(self):
        """
        L{task.react} returns quickly if the L{defer.Deferred} returned by the
        function it is passed has already been errbacked at the time it is
        returned.
        """
        class ExpectedException(Exception):
            pass

        def main(reactor):
            return defer.fail(ExpectedException())
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, _reactor=r)
        self.assertEqual(1, exitError.code)
        self.assertEqual(r.seconds(), 0)
        errors = self.flushLoggedErrors(ExpectedException)
        self.assertEqual(len(errors), 1) 
Example #3
Source File: test_task.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_runsUntilAsyncErrback(self):
        """
        L{task.react} runs the reactor until the L{defer.Deferred} returned by
        the function it is passed is errbacked, then it stops the reactor and
        reports the error.
        """
        class ExpectedException(Exception):
            pass

        def main(reactor):
            finished = defer.Deferred()
            reactor.callLater(1, finished.errback, ExpectedException())
            return finished
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, _reactor=r)

        self.assertEqual(1, exitError.code)

        errors = self.flushLoggedErrors(ExpectedException)
        self.assertEqual(len(errors), 1) 
Example #4
Source File: test_task.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_singleStopCallback(self):
        """
        L{task.react} doesn't try to stop the reactor if the L{defer.Deferred}
        the function it is passed is called back after the reactor has already
        been stopped.
        """
        def main(reactor):
            reactor.callLater(1, reactor.stop)
            finished = defer.Deferred()
            reactor.addSystemEventTrigger(
                'during', 'shutdown', finished.callback, None)
            return finished
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, _reactor=r)
        self.assertEqual(r.seconds(), 1)

        self.assertEqual(0, exitError.code) 
Example #5
Source File: test_task.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_runsUntilAsyncCallback(self):
        """
        L{task.react} runs the reactor until the L{Deferred} returned by the
        function it is passed is called back, then stops it.
        """
        timePassed = []
        def main(reactor):
            finished = defer.Deferred()
            reactor.callLater(1, timePassed.append, True)
            reactor.callLater(2, finished.callback, None)
            return finished
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, _reactor=r)
        self.assertEqual(0, exitError.code)
        self.assertEqual(timePassed, [True])
        self.assertEqual(r.seconds(), 2) 
Example #6
Source File: test_task.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_synchronousStop(self):
        """
        L{task.react} handles when the reactor is stopped just before the
        returned L{Deferred} fires.
        """
        def main(reactor):
            d = defer.Deferred()
            def stop():
                reactor.stop()
                d.callback(None)
            reactor.callWhenRunning(stop)
            return d
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, [], _reactor=r)
        self.assertEqual(0, exitError.code) 
Example #7
Source File: connectivity.py    From stethoscope with Apache License 2.0 6 votes vote down vote up
def main():
  parser = argparse.ArgumentParser(
    description="""Test connectivity for the plugins which support connectivity tests."""
  )

  parser.add_argument('--log-file', dest='logfile', default='connectivity.log')
  parser.add_argument('--debug', dest="debug", action="store_true", default=False)

  parser.add_argument('--namespaces', dest='namespaces', type=str, nargs='+',
                      default=DEFAULT_NAMESPACES,
                      help='Namespaces for plugins to test.')
  parser.add_argument('--plugins', dest='plugin_names', type=str, nargs='+',
                      default=None,
                      help='Names of plugins to test.')

  config = stethoscope.api.factory.get_config()
  args = parser.parse_args()

  config['LOGBOOK'] = stethoscope.utils.setup_logbook(args.logfile)
  config['LOGBOOK'].push_application()

  config['DEBUG'] = args.debug
  config['TESTING'] = args.debug

  task.react(_main, (args, config)) 
Example #8
Source File: test_task.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_singleStopCallback(self):
        """
        L{task.react} doesn't try to stop the reactor if the L{defer.Deferred}
        the function it is passed is called back after the reactor has already
        been stopped.
        """
        def main(reactor):
            reactor.callLater(1, reactor.stop)
            finished = defer.Deferred()
            reactor.addSystemEventTrigger(
                'during', 'shutdown', finished.callback, None)
            return finished
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, _reactor=r)
        self.assertEqual(r.seconds(), 1)

        self.assertEqual(0, exitError.code) 
Example #9
Source File: test_task.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_runsUntilSyncErrback(self):
        """
        L{task.react} returns quickly if the L{defer.Deferred} returned by the
        function it is passed has already been errbacked at the time it is
        returned.
        """
        class ExpectedException(Exception):
            pass

        def main(reactor):
            return defer.fail(ExpectedException())
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, _reactor=r)
        self.assertEqual(1, exitError.code)
        self.assertEqual(r.seconds(), 0)
        errors = self.flushLoggedErrors(ExpectedException)
        self.assertEqual(len(errors), 1) 
Example #10
Source File: test_task.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_runsUntilAsyncErrback(self):
        """
        L{task.react} runs the reactor until the L{defer.Deferred} returned by
        the function it is passed is errbacked, then it stops the reactor and
        reports the error.
        """
        class ExpectedException(Exception):
            pass

        def main(reactor):
            finished = defer.Deferred()
            reactor.callLater(1, finished.errback, ExpectedException())
            return finished
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, _reactor=r)

        self.assertEqual(1, exitError.code)

        errors = self.flushLoggedErrors(ExpectedException)
        self.assertEqual(len(errors), 1) 
Example #11
Source File: test_task.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_runsUntilAsyncCallback(self):
        """
        L{task.react} runs the reactor until the L{Deferred} returned by the
        function it is passed is called back, then stops it.
        """
        timePassed = []
        def main(reactor):
            finished = defer.Deferred()
            reactor.callLater(1, timePassed.append, True)
            reactor.callLater(2, finished.callback, None)
            return finished
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, _reactor=r)
        self.assertEqual(0, exitError.code)
        self.assertEqual(timePassed, [True])
        self.assertEqual(r.seconds(), 2) 
Example #12
Source File: run.py    From stethoscope with Apache License 2.0 5 votes vote down vote up
def main():
  parser = argparse.ArgumentParser(
    description="""Pull records for a batch of users and submit to external services."""
  )
  parser.add_argument('--timeout', dest="timeout", type=int, default=10)
  parser.add_argument('--limit', dest="limit", type=int, default=10,
      help="""Retrieve data for at most this many users concurrently.""")

  parser.add_argument('--log-file', dest='logfile', default='batch.log')

  parser.add_argument('input', nargs='?', type=argparse.FileType('r'), default=None)

  parser.add_argument('--collect-only', dest="collect_only", action="store_true")
  parser.add_argument('--debug', dest="debug", action="store_true", default=False)

  config = stethoscope.api.factory.get_config()
  args = parser.parse_args()

  for plugin in ['BITFIT', 'JAMF']:
    config[plugin + '_TIMEOUT'] = args.timeout

  config['LOGBOOK'] = stethoscope.utils.setup_logbook(args.logfile)
  config['LOGBOOK'].push_application()

  config['DEBUG'] = args.debug
  config['TESTING'] = args.debug

  yaml.add_representer(arrow.arrow.Arrow, arrow_representer)
  yaml.SafeDumper.add_representer(arrow.arrow.Arrow, arrow_representer)

  task.react(_main, (args, config)) 
Example #13
Source File: test_task.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_asynchronousStop(self):
        """
        L{task.react} handles when the reactor is stopped and the
        returned L{Deferred} doesn't fire.
        """
        def main(reactor):
            reactor.callLater(1, reactor.stop)
            return defer.Deferred()
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, [], _reactor=r)
        self.assertEqual(0, exitError.code) 
Example #14
Source File: __main__.py    From imaginary with MIT License 5 votes vote down vote up
def main(argv):
    """
    Start logging to a temporary file and then run the main loop.
    """
    startLogging(file('imaginary-debug.log', 'w'))
    withSavedTerminalSettings(sys.__stdin__.fileno(),
                              lambda: react(runTextServer, argv)) 
Example #15
Source File: test_task.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_exitWithDefinedCode(self):
        """
        L{task.react} forwards the exit code specified by the C{SystemExit}
        error returned by the passed function, if any.
        """
        def main(reactor):
            return defer.fail(SystemExit(23))
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, [], _reactor=r)
        self.assertEqual(23, exitError.code) 
Example #16
Source File: test_task.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_defaultReactor(self):
        """
        L{twisted.internet.reactor} is used if no reactor argument is passed to
        L{task.react}.
        """
        def main(reactor):
            self.passedReactor = reactor
            return defer.succeed(None)

        reactor = _FakeReactor()
        with NoReactor():
            installReactor(reactor)
            exitError = self.assertRaises(SystemExit, task.react, main, [])
            self.assertEqual(0, exitError.code)
        self.assertIs(reactor, self.passedReactor) 
Example #17
Source File: test_task.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_arguments(self):
        """
        L{task.react} passes the elements of the list it is passed as
        positional arguments to the function it is passed.
        """
        args = []
        def main(reactor, x, y, z):
            args.extend((x, y, z))
            return defer.succeed(None)
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, [1, 2, 3], _reactor=r)
        self.assertEqual(0, exitError.code)
        self.assertEqual(args, [1, 2, 3]) 
Example #18
Source File: crawl.py    From oxidizr with GNU General Public License v2.0 5 votes vote down vote up
def handle(self, *args, **options):
        for site in Website.objects.filter(is_deep_crawl=True):
            site.last_crawled_at = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
            site.save()
            task.react(main, [str(site.url)]) # Can pass a list of urls 
Example #19
Source File: test_task.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_runsUntilSyncCallback(self):
        """
        L{task.react} returns quickly if the L{Deferred} returned by the
        function it is passed has already been called back at the time it is
        returned.
        """
        def main(reactor):
            return defer.succeed(None)
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, _reactor=r)
        self.assertEqual(0, exitError.code)
        self.assertEqual(r.seconds(), 0) 
Example #20
Source File: test_script.py    From flocker with Apache License 2.0 5 votes vote down vote up
def test_react(self):
        """
        `FlockerScriptRunner._react` is ``task.react`` by default
        """
        self.assertIs(
            task.react,
            FlockerScriptRunner(script=None, options=None)._react
        ) 
Example #21
Source File: script.py    From flocker with Apache License 2.0 5 votes vote down vote up
def main_for_service(reactor, service):
    """
    Start a service and integrate its shutdown with reactor shutdown.

    This is useful for hooking driving an ``IService`` provider with
    ``twisted.internet.task.react``.  For example::

        from twisted.internet.task import react
        from yourapp import YourService
        react(_main_for_service, [YourService()])

    :param IReactorCore reactor: The reactor the run lifetime of which to tie
        to the given service.  When the reactor is shutdown, the service will
        be shutdown.

    :param IService service: The service to tie to the run lifetime of the
        given reactor.  It will be started immediately and made to stop when
        the reactor stops.

    :return: A ``Deferred`` which fires after the service has finished
        stopping.
    """
    service.startService()
    stop = Deferred()
    reactor.addSystemEventTrigger(
        "before", "shutdown", _chain_stop_result, service, stop)
    return stop 
Example #22
Source File: script.py    From flocker with Apache License 2.0 5 votes vote down vote up
def main(self):
        """Parse arguments and run the script's main function via ``react``."""
        # If e.g. --version is called this may throw a SystemExit, so we
        # always do this first before any side-effecty code is run:
        options = self._parse_options(self.sys_module.argv[1:])

        if self.logging:
            log_writer = eliot_logging_service(
                options.eliot_destination, self._reactor, True
            )
        else:
            log_writer = Service()
        log_writer.startService()

        # XXX: We shouldn't be using this private _reactor API. See
        # https://twistedmatrix.com/trac/ticket/6200 and
        # https://twistedmatrix.com/trac/ticket/7527
        def run_and_log(reactor):
            d = maybeDeferred(self.script.main, reactor, options)

            def got_error(failure):
                if failure.check(UsageError):
                    err(failure.value.args)
                    raise SystemExit(1)
                elif not failure.check(SystemExit):
                    err(failure)
                return failure
            d.addErrback(got_error)
            return d
        try:
            self._react(run_and_log, [], _reactor=self._reactor)
        finally:
            log_writer.stopService() 
Example #23
Source File: test_task.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_asynchronousStop(self):
        """
        L{task.react} handles when the reactor is stopped and the
        returned L{Deferred} doesn't fire.
        """
        def main(reactor):
            reactor.callLater(1, reactor.stop)
            return defer.Deferred()
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, [], _reactor=r)
        self.assertEqual(0, exitError.code) 
Example #24
Source File: test_task.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_exitWithDefinedCode(self):
        """
        L{task.react} forwards the exit code specified by the C{SystemExit}
        error returned by the passed function, if any.
        """
        def main(reactor):
            return defer.fail(SystemExit(23))
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, [], _reactor=r)
        self.assertEqual(23, exitError.code) 
Example #25
Source File: test_task.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_defaultReactor(self):
        """
        L{twisted.internet.reactor} is used if no reactor argument is passed to
        L{task.react}.
        """
        def main(reactor):
            self.passedReactor = reactor
            return defer.succeed(None)

        reactor = _FakeReactor()
        with NoReactor():
            installReactor(reactor)
            exitError = self.assertRaises(SystemExit, task.react, main, [])
            self.assertEqual(0, exitError.code)
        self.assertIs(reactor, self.passedReactor) 
Example #26
Source File: test_task.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_arguments(self):
        """
        L{task.react} passes the elements of the list it is passed as
        positional arguments to the function it is passed.
        """
        args = []
        def main(reactor, x, y, z):
            args.extend((x, y, z))
            return defer.succeed(None)
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, [1, 2, 3], _reactor=r)
        self.assertEqual(0, exitError.code)
        self.assertEqual(args, [1, 2, 3]) 
Example #27
Source File: test_task.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_runsUntilSyncCallback(self):
        """
        L{task.react} returns quickly if the L{Deferred} returned by the
        function it is passed has already been called back at the time it is
        returned.
        """
        def main(reactor):
            return defer.succeed(None)
        r = _FakeReactor()
        exitError = self.assertRaises(
            SystemExit, task.react, main, _reactor=r)
        self.assertEqual(0, exitError.code)
        self.assertEqual(r.seconds(), 0)