ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
6

Python Spin Once Equivalent

asked 2013-12-13 12:56:33 -0500

atp gravatar image

updated 2014-01-28 17:06:37 -0500

ngrennan gravatar image

Is there an equivalent to spin_once (C++) in ROS for Python?

In C++ I have a loop with spin_once and during each iteration of the loop the code checks a couple of global variables for changes and then does some action based on the value of those global variables. The global variables are based on messages from other nodes' topics to which my C++ ROS node is subscribed. How would this code structure translate into Python?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
10

answered 2013-12-16 00:07:25 -0500

demmeln gravatar image

No, there is no spin_once in rospy. In rospy the subscriber's callbacks are executed in separate threads. Google quickly produces these old questions including answers:

To translate your loop from C++ you could either check periodically in a loop in the main thread, or check in each of the callbacks. Either way you likely need some thread synchronization.

edit flag offensive delete link more

Comments

Nabble doesn't work any longer, but I was able to find this one (it's nice the nabble url contained some searchable text, though this once has a date encoded) http://lists.ros.org/lurker/message/2...

You don't need spinOnce in rospy as the I/O is multithreaded. You can use the Rate object to contrain the rate of processing, though it does not have the same effect as spinOnce in roscpp. - Ken

lucasw gravatar image lucasw  ( 2022-01-12 10:48:03 -0500 )edit
3

answered 2019-06-12 09:00:07 -0500

byque gravatar image

The rospy function wait_for_message() allowed me to receive one message from topic in Python.

edit flag offensive delete link more

Comments

Exactly what I wanted, thanks.

I have used below code:

msg = rospy.wait_for_message("/camera/depth/color/points", PointCloud2, timeout=None)
askkvn gravatar image askkvn  ( 2020-05-19 03:24:48 -0500 )edit
2

Please note that wait_for_message does the following internally: it subscribes to the topic, waits for a message, then unsubscribes. This is only useful if you want to get an odd message once in a while. Don't do this in a loop - it will stress the ros master and all nodes publishing on that topic, and you'll lose all the messages between unsubscribing and subscribing again.

Martin Günther gravatar image Martin Günther  ( 2020-10-02 03:37:38 -0500 )edit

Question Tools

3 followers

Stats

Asked: 2013-12-13 12:56:33 -0500

Seen: 43,961 times

Last updated: Dec 16 '13