forked from platypush/platypush
Guard the start of the data throttler thread with a lock to prevent multiple launches
This commit is contained in:
parent
cd52128e44
commit
423fc492cf
1 changed files with 29 additions and 23 deletions
|
@ -2,7 +2,7 @@ import ast
|
|||
import statistics
|
||||
import time
|
||||
|
||||
from threading import Thread
|
||||
from threading import Thread, Lock
|
||||
from Adafruit_IO import Client
|
||||
from Adafruit_IO.errors import ThrottlingError
|
||||
|
||||
|
@ -10,6 +10,8 @@ from platypush.context import get_backend
|
|||
from platypush.message import Message
|
||||
from platypush.plugins import Plugin, action
|
||||
|
||||
data_throttler_lock = Lock()
|
||||
|
||||
class AdafruitIoPlugin(Plugin):
|
||||
"""
|
||||
This plugin allows you to interact with the Adafruit IO
|
||||
|
@ -62,9 +64,10 @@ class AdafruitIoPlugin(Plugin):
|
|||
self.aio = Client(username=username, key=key)
|
||||
self.throttle_seconds = throttle_seconds
|
||||
|
||||
if self.throttle_seconds:
|
||||
if self.throttle_seconds and not data_throttler_lock.locked():
|
||||
redis = self._get_redis()
|
||||
self.logger.info('Starting Adafruit IO throttler thread')
|
||||
data_throttler_lock.acquire(False)
|
||||
self.data_throttler = Thread(target=self._data_throttler())
|
||||
self.data_throttler.start()
|
||||
|
||||
|
@ -85,6 +88,7 @@ class AdafruitIoPlugin(Plugin):
|
|||
last_processed_batch_timestamp = None
|
||||
data = {}
|
||||
|
||||
try:
|
||||
while True:
|
||||
try:
|
||||
new_data = ast.literal_eval(
|
||||
|
@ -111,6 +115,8 @@ class AdafruitIoPlugin(Plugin):
|
|||
time.sleep(self.throttle_seconds)
|
||||
|
||||
data = {}
|
||||
except Exception as e:
|
||||
self.logger.exception(e)
|
||||
|
||||
return run
|
||||
|
||||
|
|
Loading…
Reference in a new issue