Multithreading Example#

The goal of this demo is to show how to use different threads to interact with FURY. In particular, the main thread is used to update interactions and render the scene, while thread A rotates the camera, thread B prints a counter, and thread C adds and removes elements from the scene.

Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Counter: 5
Counter: 6
Counter: 7
Counter: 8
Counter: 9
Counter: 10
Counter: 11
Counter: 12
Counter: 13
Counter: 14
Counter: 15
Counter: 16
Counter: 17
Counter: 18
Counter: 19
Counter: 20
Counter: 21
Counter: 22
Counter: 23
Counter: 24
Counter: 25
Counter: 26
Counter: 27
Counter: 28
Counter: 29
Counter: 30
Counter: 31
Counter: 32
Counter: 33
Counter: 34
Counter: 35
Counter: 36
Counter: 37
Counter: 38
Counter: 39
Counter: 40
Counter: 41
Counter: 42
Counter: 43
Counter: 44
Counter: 45
Counter: 46
Counter: 47
Counter: 48
Counter: 49
Counter: 50
Counter: 51
Counter: 52
Counter: 53
Counter: 54
Counter: 55
Counter: 56
Counter: 57
Counter: 58
Counter: 59
Counter: 60
Counter: 61
Counter: 62
Counter: 63
Counter: 64
Counter: 65
Counter: 66
Counter: 67
Counter: 68
Counter: 69
Counter: 70
Counter: 71
Counter: 72
Counter: 73
Counter: 74
Counter: 75
Counter: 76
Counter: 77
Counter: 78
Counter: 79
Counter: 80
Counter: 81
Counter: 82
Counter: 83
Counter: 84
Counter: 85
Counter: 86
Counter: 87
Counter: 88
Counter: 89
Counter: 90
Counter: 91
Counter: 92
Counter: 93
Counter: 94
Counter: 95
Counter: 96
Counter: 97
Counter: 98
Counter: 99

import time
from threading import Thread

import numpy as np

from fury import actor, ui, window

# Preparing to draw some spheres
xyz = 10 * (np.random.random((100, 3)) - 0.5)
colors = np.random.random((100, 4))
radii = np.random.random(100) + 0.5

scene = window.Scene()
sphere_actor = actor.sphere(
    centers=xyz, colors=colors, radii=radii, use_primitive=False
)
scene.add(sphere_actor)


# Preparing the show manager as usual
showm = window.ShowManager(
    scene, size=(900, 768), reset_camera=False, order_transparent=True
)

# showm.initialize()

# Creating a text block to show a message and reset the camera
tb = ui.TextBlock2D(bold=True)
scene.add(tb)
scene.ResetCamera()


# Create a function to print a counter to the console
def print_counter():
    print('')
    for i in range(100):
        print('\rCounter: %d' % i, end='')
        message = "Let's count up to 100 and exit :" + str(i + 1)
        tb.message = message
        time.sleep(0.05)
        if showm.is_done():
            break
    showm.exit()
    print('')


# Create a function to rotate the camera


def rotate_camera():
    for i in range(100):
        if showm.lock_current():
            scene.azimuth(0.01 * i)
            showm.release_current()
            time.sleep(0.05)
        else:
            break


# Create a function to add or remove the axes and increase its scale


def add_remove_axes():
    current_axes = None
    for i in range(100):
        if showm.lock_current():
            if current_axes is None:
                current_axes = actor.axes(scale=(0.20 * i, 0.20 * i, 0.20 * i))
                scene.add(current_axes)
                pass
            else:
                scene.rm(current_axes)
                current_axes = None
                pass
            showm.release_current()
            time.sleep(0.1)
        else:
            break


# Start the threads
# Multiple threads can be started here
# First, one to rotate the camera
thread_a = Thread(target=rotate_camera)
thread_a.start()

# Now let's start a thread that will print a counter
thread_b = Thread(target=print_counter)
thread_b.start()

# Now let's start a thread that will add or remove axes
thread_c = Thread(target=add_remove_axes)
thread_c.start()

# Let's start the show manager loop with multithreading option
showm.start(multithreaded=True)

# Wait for the threads to finish
thread_a.join()
thread_b.join()
thread_c.join()

Total running time of the script: (0 minutes 5.510 seconds)

Gallery generated by Sphinx-Gallery