Page 1 of 1

Threading conflict on Python

Posted: Monday 23 October 2017 3:34
by brunonar
I need to create a function that reads everytime a serial port to get a device notification, then I can trigger the plugin's onHeartbeat() function. For some reason the notification on serial port doesn't wait on buffer for more than a half of a second. In order to accomplish that I tried to apply a Thread(target=self.serialReader) method, and it works nicely when I isolate the script. But when I use it with Domoticz, it freezes on exit, even when I put the self.serial_task.join() on onStop or on a onDisconnect trigger.
I'd like to know how to proper use multitasking on a Domoticz python plugin.

Thanks in advance.

Re: Threading conflict on Python

Posted: Monday 23 October 2017 8:11
by Dnpwwo
@brunonar,

You haven't posted your code so I can't give specifics but general feedback is: the framework does not support multi-threading operations as stated in the wiki page (http://www.domoticz.com/wiki/Developing_a_Python_plugin):
The following things should not be attempted using the Python Framework:
  • Use of asynchronous code or libraries. This will not work the way you expect.
  • Use of call back functions. This will not work the way you expect.
  • Waiting or sleeping. Plugins are single threaded so the whole plugin system will wait.
also the callbacks are normally called by Domoticz rather than the Python itself.

To allow the usage pattern that you are describing the framework offers asynchronous objects to handle connectivity to external devices. Have a look at this example to see how to receive async messages on a serial port:https://github.com/domoticz/domoticz/bl ... s/RAVEn.py

Re: Threading conflict on Python

Posted: Monday 23 October 2017 13:33
by brunonar
Thank you very much. I'll try the example you provided, but I took a quick look at it and that seems to be what I needed.