Threading in python ZMQ API

Typically: "How do I... ", "How can I... " questions
Post Reply
caioconti
Posts: 8
Joined: 22 Apr 2024, 14:23

Threading in python ZMQ API

Post by caioconti »

I'm using threads in my code to improve time efficiency. However, I’m not sure if my approach follows best practices, even though it seems to work. Does anyone with more experience know the best practices for working with threads in CoppeliaSim using the ZMQ API in python?

Here's how I'm currently handling it: Since ZMQ is not thread-safe, each thread has its own client. I manage communication between threads simply using global variables. Example:

Code: Select all

# python
from coppeliasim_zmqremoteapi_client import RemoteAPIClient
import threading

global currentGoal

def control_loop():
    client = RemoteAPIClient()
    sim = client.require('sim')
    > getting handles
    while True:
    	> starts control loop
    	> drive robot to currentGoal

def other_thread():
    client = RemoteAPIClient()
    sim = client.require('sim')
    > getting handles
    while True:
    	> do something

if __name__ == '__main__':
    client = RemoteAPIClient()
    sim = client.require('sim')
    > getting handles
    > starts threads
    while True:
    	> update currentGoal

Are there better alternatives for handling threading? Other approaches I considered but haven't tried yet include using classes or running separate scripts concurrently. Of course, I could use ROS, but I’d like to keep the code relatively simple and easy to run using only Python and CoppeliaSim.
coppelia
Site Admin
Posts: 10738
Joined: 14 Dec 2012, 00:25

Re: Threading in python ZMQ API

Post by coppelia »

Hello,

I think that looks right, have also a look at the example script in programming/zmqRemoteApi/clients/python/simpleMovement.py

Cheers
Post Reply