May need to ditch threads in favor of async

This commit is contained in:
James Barnsley
2019-12-30 21:36:07 +13:00
parent d522316ee2
commit 7b97d3a603

View File

@ -15,7 +15,7 @@ class IrisSystemThread(Thread):
##
# Run the defined action
##
async def run(self):
def run(self):
logger.info("Running system action '"+self.action+"'")
try:
@ -32,31 +32,18 @@ class IrisSystemThread(Thread):
self.callback(False, error)
return
# Create subprocess
process = await asyncio.create_subprocess_shell(
self.path+"/system.sh "+self.action,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
# Status
print("Started:", command, "(pid = " + str(process.pid) + ")", flush=True)
# Wait for the subprocess to finish
stdout, stderr = await process.communicate()
# Run the actual task (this is the process-blocking instruction)
output = subprocess.check_output([self.path+"/system.sh "+self.action], shell=True)
logger.debug("System action '"+self.action+"' completed with output:")
logger.debug(stdout)
logger.debug(output)
# Some kind of failure, so we can't run any commands this way
if exitCode > 0:
raise Exception("System task '"+self.action+"' failed")
else:
if self.callback:
response = {
'output': str(stdout)
}
self.callback(response, False)
# And then, when complete, return to our callback
if self.callback:
response = {
'output': str(output)
}
self.callback(response, False)
##