Subscriber callback thread interrupting the main process

asked 2020-06-11 09:04:16 -0500

azerila gravatar image

I have an issue with a code base which I am writing a simplified version of it that I hope may show where the problem is. I have a subscriber which is called at ~1kHz by thread1 and this is interrupting the functionality of the main thread. My hypothesis is that and since this thread and the main thread are likely owned by the same process this interruption of the main thread happes.


I have python class (obj1) that communicates with another thread(thread1) where Thread1 calls the callback function of it at 1kHz frequency, through self._listener.

class obj1(object):
    def __init__(self):


        self._listener = rospy.Publisher('/topic1', MsgType, queue_size=1)
        self._writer  = rospy.Subscriber('/topic2', MsgType, self.callback, queue_size=1)

    def callback(self, msg1):  # called by thread1 at 1 kHz

       # updating a number of python variables and calling some methods
       # in the end:

In addition I have another class (obj2) which does something completely independent to the above object which if I use it like the following without instantiating obj1, it works with no problem at high speed, that is it func1 is called at ~2kHz by the main_thread.

class obj2 (object):
    def __init__(self):
    def func1(self): # called at 2kHz by main_thread (the main python thread)

If I instantiate obj1 in another terminal and obj2 like above in a separate terminal, there is no problem in terms of speed. but if I also instantiate obj1 inside obj2(while obj1 callback is still called by thread1), the performance of obj2 decreases, that is func1 is called at a lower frequency by the main_thread.

class obj2 (object):
    def __init__(self):
        o = obj1()
    def func1(self):  # supposed to be called at 2kHz by main_thread (the main python thread)

I am rather confused how obj2' performance is interrupted, because obj2's func1 is called by main_thread always and the callback of obj1 is called by thread1.

my understanding is that since in the above case obj2 is instantiated by the main_thread, obj1 and its python variables are also instantiated by the main_thread.

What can be the reason? Is there a way to make obj1's functionality not to interrupt obj2's and/or make their process more independent not to interrupt eachother?

edit retag flag offensive close merge delete



I would not be surprised you're running into issues caused by the GIL. And running any Python at over 1 kHz is not trivial, especially not on regular consumer Linux setups.

Threading in Python is not the same as threading in C/C++. The GIL serialises a lot of execution flows, so it may all be essentially running single-threaded.

gvdhoorn gravatar image gvdhoorn  ( 2020-06-11 09:36:56 -0500 )edit

@gvdhoorn do you think there might be a solution for it? to for example make their processes more independent while still having access to obj1 attributes.

azerila gravatar image azerila  ( 2020-06-12 02:34:31 -0500 )edit