ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Disabling parallelism of subscribers in Python

asked 2020-07-03 04:31:03 -0600

omercan_s gravatar image

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):
        print("Callback1: %s" % (

def callback2(msg):
        print("Callback2: %s" % (

def main():
    rospy.Subscriber('foo1', String, callback1, queue_size=1)
    rospy.Subscriber('foo2', String, callback2, queue_size=1)

if __name__ == '__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!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-07-27 09:10:12 -0600

Mermy gravatar image

updated 2020-07-27 09:11:08 -0600

Callbacks are, by definition, parallel. I don't think there is a way to make them not parallel. What you can do is to set up a system of semaphores i.e. shared variables that block execution of the other thread until the one that is being processed finishes. Anyway, be careful when blocking callbacks, if you have a blocked callback and another message arrives, it will create another instance of your callback and you will have two blocked callbacks.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2020-07-03 04:31:03 -0600

Seen: 157 times

Last updated: Jul 27 '20