Skip to content

Commit

Permalink
Added extra docs info for submit to executor
Browse files Browse the repository at this point in the history
  • Loading branch information
Odianosen25 committed Jan 2, 2022
1 parent bff397b commit a672b76
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions appdaemon/adapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2959,9 +2959,22 @@ def submit_to_executor(self, func, *args, **kwargs):
Returns:
A Future, which can be cancelled by calling f.cancel().
Examples:
>>> f = self.submit_to_executor(self.run_request, callback=self.callback)
>>>
>>> def callback(self, kwargs):
>>> def state_cb(self, *args, **kwargs): # callback from an entity
>>> # need to run a 30 seconds task, so need to free up the thread
>>> # need to get results, so will pass a callback for it
>>> # callback can be ignored, if the result is not needed
>>> f = self.submit_to_executor(self.run_request, url, callback=self.result_callback)
>>>
>>> def run_request(self, url): # long running function
>>> import requests
>>> res = requests.get(url)
>>> return res.json()
>>>
>>> def result_callback(self, kwargs):
>>> result = kwargs["result"]
>>> self.set_state("sensor.something", state="ready", attributes=result, replace=True) # picked up by another app
>>> # <other processing that is needed>
"""

callback = kwargs.pop("callback", None)
Expand Down

0 comments on commit a672b76

Please sign in to comment.