Provides some code to help explore threads and timing in python and ROS. You should examine the examples, run them, and write your own code to further your understand threads in python. At the end there are questions for you to turn in. You can edit this file and the code provided directly to give your answers.
scripts/gil_two_threads.py
- Non-ROS python script that demonstrates the effect of the GIL on multi-threaded python code.nodes/node_square
- Performs same computation in gil_two_threads but distributed across multiple nodesnodes/time_square
- Times the computation of node_squarelaunch/node_two_square.launch
- launches the nodes used by the node_two_threads examplenodes/sub_thread
- Subscribes to a topic and sleeps for a specified period of time. Also prints diagnostic information. In conjunction with publishers that publish at different rates, you can begin to explore the nuances of subscriber threads.nodes/timers
- Starts two timers and prints diagnostic informationnodes/throughput
- Publishes messages with different queue_lengths sizes, and at different frequencies.nodes/suborder
- Subscribes, then initializes a variable. See what happens if something is already publishing...
Use the provided code, code you write, and standard ROS tools to answer the following questions.
-
Run
scripts/gil_two_threads.py
several times. How long did the single and multi-threaded computations take (use the best times among all trials)? Is the computation that uses two threads roughly twice as fast as the version that uses a single thread? Why or why not? -
Launch
node_two_square.launch
. This runs two nodes to perform the same computations asscripts/gil_two_threads.py
and a third node to time the computations.- Is this faster than
scripts/gil_two_threads.py
? Why or why not? - Under what circumstances would this code outperform
scripts/gil_two_threads.py
and when would it underperform? (Hint, think about factors such as number of CPU cores available and setup overhead).
- Is this faster than
-
Run
scripts/race_cond.py
a few times. Notice that it does not return consistent results. Your task is to use a a mutex (called a Lock in python) to fix the race condition inscripts/race_cond.py
. The variabletotal
should count precisely the total number of iterations of the loop in both threads, every time you run the code.- You can use
with lock:
to automatically acquire a lock at the beginning of the block and release it when the block ends, either normally or due to an exception being thrown.
- You can use
-
Run
nodes/timers
. After observing the behavior and seeing the output, what can you infer about the relationship between threads androspy
timers? -
What does
rospy.spin
do? You can infer a bit by commenting it out and by replacing it with an infinite while loop. Also feel free to look at the source code to answer this. -
Are
main
,callback1
andcallback2
executed on the same thread or on different threads innodes/timers
? -
Run
sub_thread
and userostopic
to publish to thesleep
topic. Is the sleeper subscriber running on the same thread as the main code? -
By manipulating the frequency at which you publish to
sleep
and the time you have the subscriber delay you can infer information about ROS's thread behavior. Write a node calledpub_thread
that uses arospy.Timer
to publish a message tosub_thread
at 1 Hz. The node should count how many messages it publishes and print out an updated count after every time it publishes.- Describe the behavior that you see when the delay time sent by
pub_thread
is shorter than 1 second - What happens when you increase the delay time sent by
pub_thread
to 2 seconds?- Does one subscriber callback complete before the next subscriber callback is started?
- After waiting for a few seconds, kill
pub_thread
but keepsub_thread
running. What happens withsub_thread
?
- Describe the behavior that you see when the delay time sent by
-
Run two instances of the
pub_thread
node. What is the relationship between subscriber threads and publishers?