|
| 1 | +# Multiprocessing Support |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The Python Protocol Gateway now supports **automatic multiprocessing** when multiple transports are configured. This provides true concurrency and complete isolation between transports, solving the "transport busy" issues that can occur with single-threaded operation. |
| 6 | + |
| 7 | +## How It Works |
| 8 | + |
| 9 | +### Automatic Detection |
| 10 | +- **Single Transport**: Uses the original single-threaded approach |
| 11 | +- **Multiple Transports**: Automatically switches to multiprocessing mode |
| 12 | + |
| 13 | +### Process Isolation |
| 14 | +Each transport runs in its own separate process, providing: |
| 15 | +- **Complete isolation** of resources and state |
| 16 | +- **True concurrent operation** - no waiting for other transports |
| 17 | +- **Independent error handling** - one transport failure doesn't affect others |
| 18 | +- **Automatic restart** of failed processes |
| 19 | + |
| 20 | +### Transport Types |
| 21 | + |
| 22 | +#### Input Transports (read_interval > 0) |
| 23 | +- Modbus RTU, TCP, etc. |
| 24 | +- Actively read data from devices |
| 25 | +- Send data to output transports via bridging |
| 26 | + |
| 27 | +#### Output Transports (read_interval <= 0) |
| 28 | +- InfluxDB, MQTT, etc. |
| 29 | +- Receive data from input transports via bridging |
| 30 | +- Process and forward data to external systems |
| 31 | + |
| 32 | +## Configuration Example |
| 33 | + |
| 34 | +```ini |
| 35 | +[transport.0] |
| 36 | +transport = modbus_rtu |
| 37 | +protocol_version = eg4_v58 |
| 38 | +address = 1 |
| 39 | +port = /dev/ttyUSB0 |
| 40 | +baudrate = 19200 |
| 41 | +bridge = influxdb_output |
| 42 | +read_interval = 10 |
| 43 | + |
| 44 | +[transport.1] |
| 45 | +transport = modbus_rtu |
| 46 | +protocol_version = eg4_v58 |
| 47 | +address = 1 |
| 48 | +port = /dev/ttyUSB1 |
| 49 | +baudrate = 19200 |
| 50 | +bridge = influxdb_output |
| 51 | +read_interval = 10 |
| 52 | + |
| 53 | +[influxdb_output] |
| 54 | +transport = influxdb_out |
| 55 | +host = influxdb.example.com |
| 56 | +port = 8086 |
| 57 | +database = solar |
| 58 | +measurement = eg4_data |
| 59 | +``` |
| 60 | + |
| 61 | +## Inter-Process Communication |
| 62 | + |
| 63 | +### Bridging |
| 64 | +- Uses `multiprocessing.Queue` for communication |
| 65 | +- Automatic message routing between processes |
| 66 | +- Non-blocking communication |
| 67 | +- Source transport information preserved |
| 68 | + |
| 69 | +### Message Format |
| 70 | +```python |
| 71 | +{ |
| 72 | + 'source_transport': 'transport.0', |
| 73 | + 'target_transport': 'influxdb_output', |
| 74 | + 'data': {...}, |
| 75 | + 'source_transport_info': { |
| 76 | + 'transport_name': 'transport.0', |
| 77 | + 'device_identifier': '...', |
| 78 | + 'device_manufacturer': '...', |
| 79 | + 'device_model': '...', |
| 80 | + 'device_serial_number': '...' |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +## Benefits |
| 86 | + |
| 87 | +### Performance |
| 88 | +- **True concurrency** - no serialization delays |
| 89 | +- **Independent timing** - each transport runs at its own interval |
| 90 | +- **No resource contention** - each process has isolated resources |
| 91 | + |
| 92 | +### Reliability |
| 93 | +- **Process isolation** - one transport failure doesn't affect others |
| 94 | +- **Automatic restart** - failed processes are automatically restarted |
| 95 | +- **Independent error handling** - each process handles its own errors |
| 96 | + |
| 97 | +### Scalability |
| 98 | +- **Linear scaling** - performance scales with number of CPU cores |
| 99 | +- **Resource efficiency** - only uses multiprocessing when needed |
| 100 | +- **Memory isolation** - each process has its own memory space |
| 101 | + |
| 102 | +## Troubleshooting |
| 103 | + |
| 104 | +### Common Issues |
| 105 | + |
| 106 | +#### "Register is Empty; transport busy?" |
| 107 | +- **Cause**: Shared state between transports in single-threaded mode |
| 108 | +- **Solution**: Use multiprocessing mode (automatic with multiple transports) |
| 109 | + |
| 110 | +#### InfluxDB not receiving data |
| 111 | +- **Cause**: Output transport not properly configured or started |
| 112 | +- **Solution**: Ensure `influxdb_output` section has `transport = influxdb_out` |
| 113 | + |
| 114 | +#### Process restarting frequently |
| 115 | +- **Cause**: Transport configuration error or device connection issue |
| 116 | +- **Solution**: Check logs for specific error messages |
| 117 | + |
| 118 | +### Debugging |
| 119 | + |
| 120 | +#### Enable Debug Logging |
| 121 | +```ini |
| 122 | +[general] |
| 123 | +log_level = DEBUG |
| 124 | +``` |
| 125 | + |
| 126 | +#### Monitor Process Status |
| 127 | +The gateway logs process creation and status: |
| 128 | +``` |
| 129 | +[2025-06-22 19:30:45] Starting multiprocessing mode with 3 transports |
| 130 | +[2025-06-22 19:30:45] Input transports: 2, Output transports: 1 |
| 131 | +[2025-06-22 19:30:45] Bridging detected - enabling inter-process communication |
| 132 | +[2025-06-22 19:30:45] Started process for transport.0 (PID: 12345) |
| 133 | +[2025-06-22 19:30:45] Started process for transport.1 (PID: 12346) |
| 134 | +[2025-06-22 19:30:45] Started process for influxdb_output (PID: 12347) |
| 135 | +``` |
| 136 | + |
| 137 | +## Testing |
| 138 | + |
| 139 | +Run the test script to verify multiprocessing functionality: |
| 140 | +```bash |
| 141 | +python pytests/test_multiprocessing.py |
| 142 | +``` |
| 143 | + |
| 144 | +This will: |
| 145 | +- Load your configuration |
| 146 | +- Display transport information |
| 147 | +- Run for 30 seconds to verify operation |
| 148 | +- Show any errors or issues |
| 149 | + |
| 150 | +## Limitations |
| 151 | + |
| 152 | +1. **Memory Usage**: Each process uses additional memory |
| 153 | +2. **Startup Time**: Slight delay when starting multiple processes |
| 154 | +3. **Inter-Process Communication**: Bridge messages have small overhead |
| 155 | +4. **Debugging**: More complex debugging due to multiple processes |
| 156 | + |
| 157 | +## Migration |
| 158 | + |
| 159 | +No migration required! Existing configurations will automatically benefit from multiprocessing when multiple transports are present. |
| 160 | + |
| 161 | +## Performance Tips |
| 162 | + |
| 163 | +1. **Stagger Read Intervals**: Use different read intervals to avoid resource contention |
| 164 | +2. **Optimize Batch Sizes**: Adjust batch sizes for faster individual reads |
| 165 | +3. **Monitor Logs**: Watch for process restarts indicating issues |
| 166 | +4. **Resource Limits**: Ensure sufficient system resources for multiple processes |
0 commit comments