| 1 |
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
""" |
| 12 |
Monitoring a dynamic number of threads |
| 13 |
|
| 14 |
Shows a simple user interface being updated by a dynamic number of threads. |
| 15 |
|
| 16 |
When the *Create Threads* button is pressed, the *count* method is |
| 17 |
dispatched on a new thread. It then creates a new *Counter* object and |
| 18 |
adds it to the *counters* list (which causes the *Counter* to appear |
| 19 |
in the user interface. It then counts by incrementing the *Counter* |
| 20 |
object's *count* trait (which again causes a user interface update each |
| 21 |
time the counter is incremented). After it reaches its maximum count, it |
| 22 |
removes the *Counter* from the *counter* list (causing the counter |
| 23 |
to be removed from the user interface) and exits (terminating the thread). |
| 24 |
|
| 25 |
Note that repeated clicking of the *Create Thread* button will create |
| 26 |
additional threads. |
| 27 |
""" |
| 28 |
|
| 29 |
from time import sleep |
| 30 |
from traits.api import HasTraits, Int, Button, List |
| 31 |
from traitsui.api import View, Item, ListEditor |
| 32 |
|
| 33 |
# -- The Counter objects used to keep track of the current count ---------- |
| 34 |
|
| 35 |
|
| 36 |
class Counter(HasTraits): |
| 37 |
|
| 38 |
# The current count: |
| 39 |
count = Int() |
| 40 |
|
| 41 |
view = View(Item('count', style='readonly')) |
| 42 |
|
| 43 |
|
| 44 |
# -- The main 'ThreadDemo' class ------------------------------------------ |
| 45 |
|
| 46 |
|
| 47 |
class ThreadDemo(HasTraits): |
| 48 |
|
| 49 |
# The button used to start a new thread running: |
| 50 |
create = Button('Create Thread') |
| 51 |
|
| 52 |
# The set of counter objects currently running: |
| 53 |
counters = List(Counter) |
| 54 |
|
| 55 |
view = View( |
| 56 |
Item('create', show_label=False), |
| 57 |
'_', |
| 58 |
Item( |
| 59 |
'counters', |
| 60 |
style='custom', |
| 61 |
show_label=False, |
| 62 |
editor=ListEditor(use_notebook=True, dock_style='tab'), |
| 63 |
), |
| 64 |
resizable=True, |
| 65 |
width=300, |
| 66 |
height=150, |
| 67 |
title='Dynamic threads', |
| 68 |
) |
| 69 |
|
| 70 |
def __init__(self, **traits): |
| 71 |
super().__init__(**traits) |
| 72 |
|
| 73 |
# Set up the notification handler for the 'Create Thread' button so |
| 74 |
# that it is run on a new thread: |
| 75 |
self.on_trait_change(self.count, 'create', dispatch='new') |
| 76 |
|
| 77 |
def count(self): |
| 78 |
"""This method is dispatched on a new thread each time the |
| 79 |
'Create Thread' button is pressed. |
| 80 |
""" |
| 81 |
counter = Counter() |
| 82 |
self.counters.append(counter) |
| 83 |
for i in range(1000): |
| 84 |
counter.count += 1 |
| 85 |
sleep(0.030) |
| 86 |
self.counters.remove(counter) |
| 87 |
|
| 88 |
|
| 89 |
# Create the demo: |
| 90 |
demo = ThreadDemo() |
| 91 |
|
| 92 |
# Run the demo (if invoked from the command line): |
| 93 |
if __name__ == '__main__': |
| 94 |
demo.configure_traits() |