0

I have an issue with a code base which I am writing a simplified version of with that I hope may show where the problem is.

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):

        rospy.init_node('node1')

        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:
       self._writer.publish(msg)  

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): # 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? Could it be that when thread1 is updating the obj1 variables, the main_thread is interrupted?

Is there a way to make obj1's functionality not to interrupt obj2's and make their process more independent?

--------------------------edit

My other hypothesis is that both threads are within the same process, which I thought it might be possible to check by printing os.getpid() inside each function. but if it is indeed the reason, the question is if we can separate the processes for obj1 and obj2

Alejandro
  • 879
  • 11
  • 27
  • 1
    It sounds like you are being affected by CPython's [Global Interpreter Lock](https://stackoverflow.com/questions/1294382/what-is-the-global-interpreter-lock-gil-in-cpython). Separating your independent threads into separate processes would indeed let you work around the limitations of the GIL. – dano Jun 11 '20 at 13:49
  • @dano how do I seperate the processes though, and still be able to access attributes of obj1.... – Alejandro Jun 11 '20 at 13:52
  • 2
    I don't know enough about your actual use-case to answer that. I would read the [`multiprocessing` documentation](https://docs.python.org/2/library/multiprocessing.html), particularly the sections about [sharing state between processes](https://docs.python.org/2/library/multiprocessing.html#sharing-state-between-processes), and [exchanging objects between processes](https://docs.python.org/2/library/multiprocessing.html#exchanging-objects-between-processes), to see if any of the approaches detailed there work for you. – dano Jun 11 '20 at 13:55

0 Answers0