Threading conflict on Python Topic is solved

Python and python framework

Moderator: leecollings

Post Reply
brunonar
Posts: 10
Joined: Monday 23 October 2017 3:22
Target OS: Windows
Domoticz version:
Contact:

Threading conflict on Python

Post 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.
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Threading conflict on Python

Post 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
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
brunonar
Posts: 10
Joined: Monday 23 October 2017 3:22
Target OS: Windows
Domoticz version:
Contact:

Re: Threading conflict on Python

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest