VPython Multithreading

From Physics Book
Jump to navigation Jump to search

Claimed by Philip Bale

Here we discuss multithreading as it relates to advanced VPython physics computations

Overview

Multithreading is the ability to execute multiple processes (or threads) concurrently. Instead of a single execution flow, multiple program paths may be followed at once. With multithreading, programs may no longer be executed strictly in a linear manner. The notion of multithreaded parallelism in computing is similar to multitasking in real life--the idea is to get more done at once.

Multithreading is very important to VPython. Imagine a Solar System model. Though it would be possible to model and update the entire system in a linear fashion, when it comes to movement and animation, the computation can get incredibly intense. Since your computer shares it's process cycles with various other programs, you may notice significant lag while trying to achieve your task. Multithreading, however, would allow you to model and update each Planet (or even subsystems of a planet, etc.) concurrently. For a system of thousands of relevant objects, the speed improvement could be many orders of magnitude with multithreading.

As an analogy, think of multithreading as a single employee vs. an assembly line. A car could simply be worked on by a singular person. It may also, however, be worked on by multiple people performing their own tasks at the same time. The idea of multiple tasks being executed at the same time is the essence of multithreading.

Advantages

Multithreading allows advanced and complex computations to be performed more efficiently due to concurrency. The asynchronous potential of multithreaded programs supports smoother graphical renderings and more accurate user input as well. While one thread is performing mathematical computations, another could be rendering models on screen, while another is taking user input. The modularity of threading allows users to break down processes as they see fit. Furthermore, various threads may share global objects. This allows for mutual cooperation and sharing when necessary. Threads are also light-weight and cheaper than processes. Since threads can be interrupted and put on hold, they also afford a good amount of flexibility.

Disadvantages

With multithreading comes risks and disadvantage--though not to be thought as zero-sum. There are cases when breaking up a single process into multiple threads is more costly due to memory and resource allocations. From a programming standpoint, multithreaded applications are also more complex in terms of time and logic. Thread scheduling can also cause program locks if not executed correctly.


Implementation

Threading Library

  • threading.currentThread()
Returns the count of threads under the control of the caller
  • threading.activeCount()
Returns the count of total active threads
  • threading.enumerate()
Returns a list of all active threads as Thread objects

Thread instance methods

  • run()
Method for the starting point of a thread
  • start()
Calls the run method of a thread
  • getName()
Returns the name of a thread
  • setName()
Sets the name of a thread
  • isAlive()
Boolean value indicating whether the thread is still running or not
  • join()
Function that waits for other threads to terminate before continuing

Thread Creation

Creating a thread is a 3 step process.

  1. Create a Thread object with a target
  2. Cache your thread for maintenance
  3. Start your thread

import threading
def ourTarget():
print 'Thread Started'
return
threads = []
t = threading.thread(target=ourTarget)
threads.append(t)
t.start()

Thread Synchronization

Thread synchronization is the process of waiting for another thread to complete before proceeding. For instance, you may not want to begin rending objects on screen until a certain computation or input is received. You may also want to wait for all threads to finish executing before terminating your program.

In python, we use the instance method "join" to tell a thread to wait for the completion of others.

import threading
from random import randrange
def ourTarget():
print 'Thread Started'
for i in range(randrange(100,10000)):
ball = sphere(pos=vector(0,0,0), color=color.red, radius = randrange(0, 100))
return
threads = []
for i in range(20):
t = threading.thread(target=ourTarget)
threads.append(t)
t.start()
for t in threads:
t.join()
print 'All threads finished'

Connectedness

  1. How is this topic connected to something that you are interested in?
With coding, I know first hand that better performance is often needed--especially when running complex simulations. I thought it would be fun to demonstrate multithreading in relation to VPython.
  1. How is it connected to your major?
As a CS major, I spend a lot of my time coding. I run into situations that need multithreading quite often.
  1. Is there an interesting industrial application?
Yes! Almost every commercial application needs multithreading! It's a core requirement to when working on applications that are nontrivial!

History

Multithreading first appeared in 1967 in OS/360 Multiprogramming with a Variable Number of Tasks or MVT. This time-sliced system allowed "tasks" to be run simultaneously. Timesharing at this point referred to multiple users sharing a single machine at different times. This MVT, however, allowed for a new definition of timesharing applications.[1]

See also

VPython Intro

VPython Basics

External links

Python Official Threading Docs

Python Thread Based Parallelism

References

This section contains the the references you used while writing this page