There are many possible solutions.
You could use one venv for your Sensors and the Motor.
If it's not possible because of incompatility of libraries, you could use IPC (inter process communication).
There are different ways: Memory Mapped file, a named PIPE, UNIX-Sockets, TCP/UDP.
You can also use libraries for IPC. I used zmq for a long time, because this does not require a server process.
The library itself runs a thread which handles the communication via TCP.
So if your motor should do anything, which depends on sensor data, you send the sensor data to your motor program.
https://pyzmq.readthedocs.io/en/latest/api/zmq.html
Example:
server.py (sensor)
client.py (motor)But there are more solutions. Another could be MQTT as message transport between the processes, but this requires a running mqtt server. On Linux mosquitto is used.
You could use one venv for your Sensors and the Motor.
If it's not possible because of incompatility of libraries, you could use IPC (inter process communication).
There are different ways: Memory Mapped file, a named PIPE, UNIX-Sockets, TCP/UDP.
You can also use libraries for IPC. I used zmq for a long time, because this does not require a server process.
The library itself runs a thread which handles the communication via TCP.
So if your motor should do anything, which depends on sensor data, you send the sensor data to your motor program.
https://pyzmq.readthedocs.io/en/latest/api/zmq.html
Example:
server.py (sensor)
Code:
import timefrom zmq import Context, PUBctx = Context()socket = ctx.socket(PUB)socket.bind("tcp://127.0.0.1:6060")while True:# sending topic and time as bytes socket.send_multipart([b"sensor", str(time.time()).encode()]) time.sleep(1)
client.py (motor)
Code:
import timefrom zmq import Context, SUB, NOBLOCK, Againctx = Context()socket = ctx.socket(SUB)socket.connect("tcp://127.0.0.1:6060")socket.subscribe(b"sensor")while True: try: msg = socket.recv_multipart(NOBLOCK) # not blocking, but raises an Exception: zmq.error.Again except Again: pass else: # if no exception -> print the received message, which # includes also the topic. All in bytes. print(msg) time.sleep(0.1)
Statistics: Posted by DeaD_EyE — Mon Jan 13, 2025 8:00 am