Disabling parallelism of subscribers in Python
Hi all, I have observed that rospy is enabling the parallelism of subscriber callbacks which means that before one callback that receives a message from a topic finishes, another callback that receives a message from another topic can start execution. This might be ideal for most applications, but in my case it is not the desired behavior. To make it simple and general I am sharing the below code that would serve as an example:
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
def callback1(msg):
while(True):
print("Callback1: %s" % (msg.data))
rospy.sleep(1)
def callback2(msg):
while(True):
print("Callback2: %s" % (msg.data))
rospy.sleep(1)
def main():
rospy.init_node('foo')
rospy.Subscriber('foo1', String, callback1, queue_size=1)
rospy.Subscriber('foo2', String, callback2, queue_size=1)
rospy.spin()
if __name__ == '__main__':
main()
And the output is like:
Callback1: qwe
Callback2: asd
Callback1: qwe
Callback2: asd
Callback1: qwe
Is there any way to force a node to use a single thread for all of its callbacks which would mean a callback blocking the others from starting to execute until it finishes? Thanks in advance!