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.pyseveral 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.pyand 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.pyand 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.pya 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 variabletotalshould 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 androspytimers? -
What does
rospy.spindo? 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,callback1andcallback2executed on the same thread or on different threads innodes/timers? -
Run
sub_threadand userostopicto publish to thesleeptopic. Is the sleeper subscriber running on the same thread as the main code? -
By manipulating the frequency at which you publish to
sleepand the time you have the subscriber delay you can infer information about ROS's thread behavior. Write a node calledpub_threadthat uses arospy.Timerto publish a message tosub_threadat 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_threadis shorter than 1 second - What happens when you increase the delay time sent by
pub_threadto 2 seconds?- Does one subscriber callback complete before the next subscriber callback is started?
- After waiting for a few seconds, kill
pub_threadbut keepsub_threadrunning. What happens withsub_thread?
- Describe the behavior that you see when the delay time sent by
-
Run two instances of the
pub_threadnode. What is the relationship between subscriber threads and publishers?