def test_max_workers(self):
     executor = TraitsExecutor(
         max_workers=11,
         context=self._context,
         event_loop=self._event_loop,
     )
     self.assertEqual(executor._worker_pool._max_workers, 11)
     executor.shutdown(timeout=SAFETY_TIMEOUT)
 def temporary_executor(self, **kwds):
     """
     Create a temporary TraitsExecutor, and shut it down properly after use.
     """
     executor = TraitsExecutor(**kwds)
     try:
         yield executor
     finally:
         executor.shutdown(timeout=SAFETY_TIMEOUT)
    def test_owned_worker_pool(self):
        executor = TraitsExecutor(
            context=self._context,
            event_loop=self._event_loop,
        )
        worker_pool = executor._worker_pool

        executor.shutdown(timeout=SAFETY_TIMEOUT)

        # Check that the internally-created worker pool has been shut down.
        with self.assertRaises(RuntimeError):
            worker_pool.submit(int)
    def test_shared_worker_pool(self):
        with self.temporary_worker_pool() as worker_pool:
            executor = TraitsExecutor(
                worker_pool=worker_pool,
                context=self._context,
                event_loop=self._event_loop,
            )
            executor.shutdown(timeout=SAFETY_TIMEOUT)

            # Check that the the shared worker pool is still usable.
            cf_future = worker_pool.submit(int)
            self.assertEqual(cf_future.result(), 0)
예제 #5
0
def main():
    """
    Demonstrate a GUI that hands off background tasks to a separate process.
    """
    context = MultiprocessingContext()
    traits_executor = TraitsExecutor(context=context)
    try:
        view = SquaringHelper(traits_executor=traits_executor)
        view.configure_traits()
    finally:
        traits_executor.shutdown()
        context.close()
    def test_thread_pool_argument_deprecated(self):
        with self.temporary_worker_pool() as worker_pool:
            with self.assertWarns(DeprecationWarning) as warning_info:
                executor = TraitsExecutor(
                    thread_pool=worker_pool,
                    context=self._context,
                    event_loop=self._event_loop,
                )
            executor.shutdown(timeout=SAFETY_TIMEOUT)

        # Check we're using the right stack level in the warning.
        _, _, this_module = __name__.rpartition(".")
        self.assertIn(this_module, warning_info.filename)
class TestTraitsExecutor(TestAssistant, TraitsExecutorTests,
                         unittest.TestCase):
    def setUp(self):
        TestAssistant.setUp(self)
        self._context = MultiprocessingContext()
        self.executor = TraitsExecutor(
            context=self._context,
            event_loop=self._event_loop,
        )
        self.listener = ExecutorListener(executor=self.executor)

    def tearDown(self):
        del self.listener
        self.executor.shutdown(timeout=SAFETY_TIMEOUT)
        del self.executor
        self._context.close()
        del self._context
        TestAssistant.tearDown(self)
예제 #8
0
class TestMyFuture(GuiTestAssistant, unittest.TestCase):
    def setUp(self):
        GuiTestAssistant.setUp(self)
        self.traits_executor = TraitsExecutor()

    def tearDown(self):
        # Request the executor to stop, and wait for that stop to complete.
        self.traits_executor.shutdown(timeout=SAFETY_TIMEOUT)
        GuiTestAssistant.tearDown(self)

    def test_my_future(self):
        future = submit_call(self.traits_executor, pow, 3, 5)

        # Wait for the future to complete.
        self.assertEventuallyTrueInGui(lambda: future.done,
                                       timeout=SAFETY_TIMEOUT)

        self.assertEqual(future.result, 243)
    def test_run_until_timeout_trait_fired(self):
        # Trait fired, but condition still never true.
        executor = TraitsExecutor(
            context=MultithreadingContext(),
            event_loop=self._event_loop,
        )
        future = submit_call(executor, int, "111")
        start_time = time.monotonic()
        with self.assertRaises(RuntimeError):
            self.run_until(
                future,
                "state",
                condition=lambda future: future.state == CANCELLED,
                timeout=0.1,
            )
        actual_timeout = time.monotonic() - start_time

        executor.shutdown(timeout=SAFETY_TIMEOUT)
        self.assertLess(actual_timeout, 1.0)
예제 #10
0
class TestTraitsExecutorWithExternalWorkerPool(
    TestAssistant, TraitsExecutorTests, unittest.TestCase
):
    def setUp(self):
        TestAssistant.setUp(self)
        self._context = MultithreadingContext()
        self._worker_pool = self._context.worker_pool()
        self.executor = TraitsExecutor(
            context=self._context,
            event_loop=self._event_loop,
            worker_pool=self._worker_pool,
        )
        self.listener = ExecutorListener(executor=self.executor)

    def tearDown(self):
        del self.listener
        self.executor.shutdown(timeout=SAFETY_TIMEOUT)
        del self.executor
        self._worker_pool.shutdown()
        del self._worker_pool
        self._context.close()
        del self._context
        TestAssistant.tearDown(self)
예제 #11
0
        )
        plot.underlays.append(pi_line)

        # Allow extra room for the y-axis label.
        plot.padding_left = 100

        return plot

    def default_traits_view(self):
        return View(
            HGroup(
                UItem("plot", editor=ComponentEditor()),
                VGroup(
                    Item("chunk_size"),
                    Item("max_points"),
                    UItem("approximate", enabled_when="approximate_enabled"),
                    UItem("cancel", enabled_when="cancel_enabled"),
                ),
            ),
            resizable=True,
        )


if __name__ == "__main__":
    traits_executor = TraitsExecutor()
    try:
        view = PiIterator(traits_executor=traits_executor)
        view.configure_traits()
    finally:
        traits_executor.shutdown()