Skip to content

Commit

Permalink
Update examples to use 'shutdown' (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdickinson committed Jul 8, 2021
1 parent 51fc65c commit 7eae70f
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 69 deletions.
2 changes: 1 addition & 1 deletion docs/source/guide/contexts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ a multiprocessing context::
view = SquaringHelper(traits_executor=traits_executor)
view.configure_traits()
finally:
traits_executor.stop()
traits_executor.shutdown()
context.close()

Here's a :download:`complete TraitsUI example
Expand Down
16 changes: 12 additions & 4 deletions docs/source/guide/examples/background_processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@
import random
import time

from traits.api import Button, Dict, Instance, List, Property, Range, Str
from traits.api import (
Button,
Dict,
HasStrictTraits,
Instance,
List,
Property,
Range,
Str,
)
from traits_futures.api import (
CallFuture,
CANCELLED,
Expand All @@ -37,7 +46,6 @@
WAITING,
)
from traitsui.api import (
Handler,
HGroup,
Item,
TabularAdapter,
Expand Down Expand Up @@ -100,7 +108,7 @@ def _get_state_text(self):
return state_text


class SquaringHelper(Handler):
class SquaringHelper(HasStrictTraits):
#: The Traits executor for the background jobs.
traits_executor = Instance(TraitsExecutor)

Expand Down Expand Up @@ -168,7 +176,7 @@ def main():
view = SquaringHelper(traits_executor=traits_executor)
view.configure_traits()
finally:
traits_executor.stop()
traits_executor.shutdown()
context.close()


Expand Down
10 changes: 7 additions & 3 deletions docs/source/guide/examples/fizz_buzz_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

class FizzBuzzUI(HasStrictTraits):
#: The executor to submit tasks to.
executor = Instance(TraitsExecutor, ())
traits_executor = Instance(TraitsExecutor)

#: The future object returned on task submission.
future = Instance(FizzBuzzFuture)
Expand All @@ -48,7 +48,7 @@ class FizzBuzzUI(HasStrictTraits):
@observe("calculate")
def _submit_calculation(self, event):
self.message = "Running"
self.future = submit_fizz_buzz(self.executor)
self.future = submit_fizz_buzz(self.traits_executor)

@observe("cancel")
def _cancel_running_task(self, event):
Expand Down Expand Up @@ -89,4 +89,8 @@ def _get_can_cancel(self):


if __name__ == "__main__":
FizzBuzzUI().configure_traits()
traits_executor = TraitsExecutor()
try:
FizzBuzzUI(traits_executor=traits_executor).configure_traits()
finally:
traits_executor.shutdown()
12 changes: 9 additions & 3 deletions docs/source/guide/examples/interruptible_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def approximate_pi(sample_count=10 ** 8):

class InterruptibleTaskExample(HasStrictTraits):
#: The executor to submit tasks to.
executor = Instance(TraitsExecutor, ())
traits_executor = Instance(TraitsExecutor)

#: The future object returned on task submission.
future = Instance(IFuture)
Expand All @@ -79,7 +79,7 @@ class InterruptibleTaskExample(HasStrictTraits):
def _submit_calculation(self, event):
self.message = "Calculating π"
self.future = submit_iteration(
self.executor, approximate_pi, self.sample_count
self.traits_executor, approximate_pi, self.sample_count
)

@observe("cancel")
Expand Down Expand Up @@ -123,4 +123,10 @@ def _get_can_cancel(self):


if __name__ == "__main__":
InterruptibleTaskExample().configure_traits()
traits_executor = TraitsExecutor()
try:
InterruptibleTaskExample(
traits_executor=traits_executor
).configure_traits()
finally:
traits_executor.shutdown()
12 changes: 9 additions & 3 deletions docs/source/guide/examples/non_interruptible_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def approximate_pi(sample_count=10 ** 8):

class NonInterruptibleTaskExample(HasStrictTraits):
#: The executor to submit tasks to.
executor = Instance(TraitsExecutor, ())
traits_executor = Instance(TraitsExecutor)

#: The future object returned on task submission.
future = Instance(IFuture)
Expand All @@ -77,7 +77,7 @@ class NonInterruptibleTaskExample(HasStrictTraits):
def _submit_calculation(self, event):
self.message = "Calculating π"
self.future = submit_call(
self.executor, approximate_pi, self.sample_count
self.traits_executor, approximate_pi, self.sample_count
)

@observe("cancel")
Expand Down Expand Up @@ -117,4 +117,10 @@ def _get_can_cancel(self):


if __name__ == "__main__":
NonInterruptibleTaskExample().configure_traits()
traits_executor = TraitsExecutor()
try:
NonInterruptibleTaskExample(
traits_executor=traits_executor
).configure_traits()
finally:
traits_executor.shutdown()
20 changes: 10 additions & 10 deletions docs/source/guide/examples/pi_iterations.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Bool,
Button,
Float,
HasStrictTraits,
Instance,
Int,
List,
Expand All @@ -36,7 +37,7 @@
submit_iteration,
TraitsExecutor,
)
from traitsui.api import Handler, HGroup, Item, UItem, VGroup, View
from traitsui.api import HGroup, Item, UItem, VGroup, View


def pi_iterations(chunk_size):
Expand Down Expand Up @@ -79,13 +80,13 @@ def pi_iterations(chunk_size):
yield nsamples, approximation, error


class PiIterator(Handler):
class PiIterator(HasStrictTraits):
"""
View and plot of pi approximation running in the background.
"""

#: The Traits executor for the background jobs.
traits_executor = Instance(TraitsExecutor, ())
traits_executor = Instance(TraitsExecutor)

#: Chunk size to use for the approximations.
chunk_size = Int(1000000)
Expand Down Expand Up @@ -117,11 +118,6 @@ class PiIterator(Handler):
#: The plot.
plot = Instance(Plot)

def closed(self, info, is_ok):
# Stopping the executor cancels any running future.
self.traits_executor.stop()
super().closed(info, is_ok)

def _approximate_fired(self):
self.future = submit_iteration(
self.traits_executor, pi_iterations, chunk_size=self.chunk_size
Expand Down Expand Up @@ -197,5 +193,9 @@ def default_traits_view(self):


if __name__ == "__main__":
view = PiIterator()
view.configure_traits()
traits_executor = TraitsExecutor()
try:
view = PiIterator(traits_executor=traits_executor)
view.configure_traits()
finally:
traits_executor.shutdown()
19 changes: 9 additions & 10 deletions docs/source/guide/examples/prime_counting.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
submit_progress,
TraitsExecutor,
)
from traitsui.api import Handler, HGroup, Item, UItem, VGroup, View
from traitsui.api import HGroup, Item, UItem, VGroup, View


class ProgressDialog(Dialog, HasStrictTraits):
Expand Down Expand Up @@ -175,13 +175,13 @@ def count_primes_less_than(n, chunk_size, progress=None):
return prime_count


class PrimeCounter(Handler):
class PrimeCounter(HasStrictTraits):
"""
UI to compute primes less than a given number.
"""

#: The Traits executor for the background jobs.
traits_executor = Instance(TraitsExecutor, ())
traits_executor = Instance(TraitsExecutor)

#: Calculation future.
future = Instance(ProgressFuture)
Expand All @@ -204,11 +204,6 @@ class PrimeCounter(Handler):
#: Limit used for most recent run.
_last_limit = Int()

def closed(self, info, is_ok):
# Stopping the executor cancels any running future.
self.traits_executor.stop()
super().closed(info, is_ok)

def _count_fired(self):
self._last_limit = self.limit
self.future = submit_progress(
Expand Down Expand Up @@ -256,5 +251,9 @@ def default_traits_view(self):


if __name__ == "__main__":
view = PrimeCounter()
view.configure_traits()
traits_executor = TraitsExecutor()
try:
view = PrimeCounter(traits_executor=traits_executor)
view.configure_traits()
finally:
traits_executor.shutdown()
11 changes: 8 additions & 3 deletions docs/source/guide/examples/quick_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def slow_square(n):

class QuickStartExample(HasStrictTraits):
#: The executor to submit tasks to.
executor = Instance(TraitsExecutor, ())
traits_executor = Instance(TraitsExecutor)

#: The future object returned on task submission.
future = Instance(CallFuture)
Expand All @@ -58,7 +58,7 @@ def _submit_background_call(self, event):
input = self.input
self.input_for_calculation = self.input
self.message = "Calculating square of {} ...".format(input)
self.future = submit_call(self.executor, slow_square, input)
self.future = submit_call(self.traits_executor, slow_square, input)
# Keep a record so that we can present messages accurately.
self.input_for_calculation = input

Expand All @@ -80,4 +80,9 @@ def _get_no_running_future(self):
)


QuickStartExample().configure_traits()
if __name__ == "__main__":
traits_executor = TraitsExecutor()
try:
QuickStartExample(traits_executor=traits_executor).configure_traits()
finally:
traits_executor.shutdown()
29 changes: 18 additions & 11 deletions docs/source/guide/examples/slow_squares.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@
import random
import time

from traits.api import Button, Dict, Instance, List, Property, Range, Str
from traits.api import (
Button,
Dict,
HasStrictTraits,
Instance,
List,
Property,
Range,
Str,
)
from traits_futures.api import (
CallFuture,
CANCELLED,
Expand All @@ -25,7 +34,6 @@
WAITING,
)
from traitsui.api import (
Handler,
HGroup,
Item,
TabularAdapter,
Expand Down Expand Up @@ -88,9 +96,9 @@ def _get_state_text(self):
return state_text


class SquaringHelper(Handler):
class SquaringHelper(HasStrictTraits):
#: The Traits executor for the background jobs.
traits_executor = Instance(TraitsExecutor, ())
traits_executor = Instance(TraitsExecutor)

#: List of the submitted jobs, for display purposes.
current_futures = List(Instance(CallFuture))
Expand All @@ -107,11 +115,6 @@ class SquaringHelper(Handler):
#: Value that we'll square.
input = Range(low=0, high=100)

def closed(self, info, is_ok):
# Cancel all jobs at shutdown.
self.traits_executor.stop()
super().closed(info, is_ok)

def _square_fired(self):
future = submit_call(self.traits_executor, slow_square, self.input)
self.current_futures.append(future)
Expand Down Expand Up @@ -158,5 +161,9 @@ def default_traits_view(self):
"%(asctime)s %(levelname)-8.8s [%(name)s:%(lineno)s] %(message)s"
),
)
view = SquaringHelper()
view.configure_traits()
traits_executor = TraitsExecutor()
try:
view = SquaringHelper(traits_executor=traits_executor)
view.configure_traits()
finally:
traits_executor.shutdown()
16 changes: 7 additions & 9 deletions docs/source/guide/examples/test_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,23 @@
class TestMyFuture(GuiTestAssistant, unittest.TestCase):
def setUp(self):
GuiTestAssistant.setUp(self)
self.executor = TraitsExecutor()
self.traits_executor = TraitsExecutor()

def tearDown(self):
# Request the executor to stop, and wait for that stop to complete.
self.executor.stop()
self.assertEventuallyTrueInGui(
lambda: self.executor.stopped, timeout=SAFETY_TIMEOUT
)

self.traits_executor.shutdown(timeout=SAFETY_TIMEOUT)
GuiTestAssistant.tearDown(self)

def test_my_future(self):
executor = self.executor

future = submit_call(executor, pow, 3, 5)
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)


if __name__ == "__main__":
unittest.main()

0 comments on commit 7eae70f

Please sign in to comment.