"Address already in use" when quickly run tinytuya script for two different devices #320
-
I have a problem when I want to control two different devices at almost the same time. I'm invoking a script as a python's subprocess.run('python run_tt.py "arg1" "arg2" "device1_id"', shell=True, text=True)
subprocess.run('python run_tt.py "arg1" "arg2" "device2_id"', shell=True, text=True) The script is simple: import sys
import tinytuya
light = sys.argv[1]
place = sys.argv[2]
tuya_id = sys.argv[3]
d = tinytuya.OutletDevice(dev_id=tuya_id)
if light == "false":
d.set_value(20, False)
else:
d.set_value(20, True) The error: Apr 06 04:47:44 rpi4 sudo[5266]: client.bind(("", UDPPORT))
Apr 06 04:47:44 rpi4 sudo[5266]: OSError: [Errno 98] Address already in use
Apr 06 08:50:27 rpi4 sudo[14327]: Traceback (most recent call last):
Apr 06 08:50:27 rpi4 sudo[14327]: File "/home/pi/workspace/pico-ble/run_tt.py", line 8, in <module>
Apr 06 08:50:27 rpi4 sudo[14327]: d = tinytuya.OutletDevice(dev_id=tuya_id)
Apr 06 08:50:27 rpi4 sudo[14327]: File "/home/pi/.local/lib/python3.9/site-packages/tinytuya/core.py", line 745, in __init__
Apr 06 08:50:27 rpi4 sudo[14327]: bcast_data = find_device(dev_id)
Apr 06 08:50:27 rpi4 sudo[14327]: File "/home/pi/.local/lib/python3.9/site-packages/tinytuya/core.py", line 527, in find_device
Apr 06 08:50:27 rpi4 sudo[14327]: client.bind(("", UDPPORT))
Apr 06 08:50:27 rpi4 sudo[14327]: OSError: [Errno 98] Address already in use I guess, it's something with using the same IP Address, but what's bothering me is that these two devices have different IPs associated, right? So what's the reason for this issue? Could you help? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
The issue is you are not specifying the IP address of the device. This causes both threads to try and listen for the device broadcast packets at the same time. We can work around the "Address already in use" error by setting the SO_REUSEPORT socket option, however that will not help in your case as devices only broadcast once every 5 seconds thereby causing a random delay before your devices will react to the commands you're sending. Instead, you will need to either include the IPs in your script, or do a scan from your main thread and pass the IPs to the subprocess. To do a scan for just a few specific devices: import tinytuya.scanner
want_devices = ['eb...1', 'eb...2', ...]
found = tinytuya.scanner.devices( verbose=False, scantime=8, poll=False, byID=True, wantids=want_devices ) Simply run the above fairly early in the main thread to find the IPs for your devices. Note that offline or not-broadcasting devices will not be found and will thus be missing from the |
Beta Was this translation helpful? Give feedback.
-
Thanks @uzlonewolf for your time in answering my questions. |
Beta Was this translation helpful? Give feedback.
The issue is you are not specifying the IP address of the device. This causes both threads to try and listen for the device broadcast packets at the same time. We can work around the "Address already in use" error by setting the SO_REUSEPORT socket option, however that will not help in your case as devices only broadcast once every 5 seconds thereby causing a random delay before your devices will react to the commands you're sending. Instead, you will need to either include the IPs in your script, or do a scan from your main thread and pass the IPs to the subprocess. To do a scan for just a few specific devices: