1

I am trying to determine which parts of my python code are running the slowest, so that I have a better understanding on what I need to fix. I recently discovered cProfile and gprof2dot which have been extremely helpful. My problem is that I'm not seeing any information about functions that I'm using as callbacks, which I believe might be running very slowly. From what I understand from this answer is that cProfile only works by default in the main thread, and I'm guessing that callbacks use a separate thread. That answer showed a way to get things working if you are using the threading library, but I couldn't get it to work for my case.

Here is roughly what my code looks like:

import rospy
import cv
import cProfile
from numpy import *
from collections import deque

class Bla():
  def __init__( self ):
    self.image_data = deque()
    self.do_some_stuff()
  def vis_callback( self, data ):
    cv_im = self.bridge.imgmsg_to_cv( data, "mono8" )
    im = asarray( cv_im )
    self.play_with_data( im )
    self.image_data.append( im )
  def run( self ):
    rospy.init_node( 'bla', anonymous=True )
    sub_vis = rospy.Subscriber('navbot/camera/image',Image,self.vis_callback)
    while not rospy.is_shutdown():
      if len( self.image_data ) > 0:
        im = self.image_data.popleft()
        self.do_some_things( im )

def main():
  bla = Bla()
  bla.run()

if __name__ == "__main__":
  cProfile.run( 'main()', 'pstats.out' ) # this could go here, or just run python with -m cProfile
  #main()

Any ideas on how to get cProfile info on the vis_callback function? Either by modifying the script itself, or even better using python -m cProfile or some other command line method?

I'm guessing it is either the reading of images or appending them to the queue which slow. My gut feeling is that storing them on a queue is a horrible thing to do, but I need to display them with matplotlib, which refuses to work if it isn't in the main thread, so I want to see exactly how bad the damage is with this workaround

Community
  • 1
  • 1
Brent
  • 719
  • 2
  • 9
  • 18

0 Answers0