Using multithreading to initialize & run several nodes with rospy.
Using: Ros Melodic on Ubuntu 18.04.
I'm trying to give navigation goals to several robots simultaneously using rospy.
As of right now, every robot in the "task queue" will wait until the one before it has completed its task.
To fix that, I thought multithreading would be useful (if you have better suggestions, please shoot!)
When running the following line (without threading):
def function(x, y, robot):
rospy.init_node('movebase_client', anonymous=True)
Everything works....
But when theading is introduced, the system breaks.
Here is a totally stripped down version of the code (This can't even run on its own.)
#!/usr/bin/env python
import rospy
import threading
def function(x, y, robot):
rospy.init_node('movebase_client', anonymous=True)
print(x, y, robot)
testing = threading.Thread(target=function, args=[3, 3, "Bot1"])
testing.start()
When running this code, I get the following error:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/home/jubuntu/multiple_costs/P5/src/deploy/launch/jacobtest.py", line 6, in function
rospy.init_node('movebase_client', anonymous=True)
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/client.py", line 285, in init_node
rospy.core.register_signals() # add handlers for SIGINT/etc...
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/core.py", line 623, in register_signals
_signalChain[signal.SIGTERM] = signal.signal(signal.SIGTERM, _ros_signal)
ValueError: signal only works in main thread
Any help would be highly appreciated!