Skip to content

Commit

Permalink
proper task cancelation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodriguez committed Nov 8, 2023
1 parent 472893d commit dae126c
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 63 deletions.
28 changes: 2 additions & 26 deletions platform/panduza_platform/core/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ def run(self):
def load_task(self, coro, name = None):
"""Load a new task into the event loop
"""
self.task_group.create_task( coro, name=name )
return self.task_group.create_task( coro, name=name )

# ---

def load_worker(self, worker):
"""Load worker into the event loop
"""
self.log.info(f"Load '{worker}'")
self.load_task( worker.task(), name=worker.PZA_WORKER_name() )
self.load_task( worker.task(), name=f"WORKER>{worker.PZA_WORKER_name()}" )

# ---

Expand Down Expand Up @@ -316,7 +316,6 @@ def __oper_mode(self):
This mode start the main event loop then the initialisation task
"""

try:
# Manage the status file (file to indicate the admin interface logs of the crash)
if os.path.isfile(STATUS_FILE_PATH):
Expand All @@ -333,29 +332,6 @@ def __oper_mode(self):
self.event_loop = asyncio.get_event_loop()
self.event_loop.run_until_complete(self.__idle_task())




# self.__stop()

# # Run all the interfaces on differents threads
# thread_id=0
# for interface in self.interfaces:
# t = threading.Thread(target=interface["instance"].start, name="T" + str(thread_id))
# thread_id+=1
# self.threads.append(t)

# # Start all the threads
# for thread in self.threads:
# thread.start()

# # Log
# self.log.info("Platform started!")

# # Join them all !
# for thread in self.threads:
# thread.join()

except InitializationError as e:
self.log.critical(f"Error during platform initialization: {e}")
self.generate_early_status_report(str(e))
Expand Down
44 changes: 19 additions & 25 deletions platform/panduza_platform/core/platform_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ def initialize(self, device, group_name):
self.device_name = self.device.get_name()
self.group_name = group_name

# Store tasks managed by this interface
self._tasks = []

# Flag to know if the topics have been subscribed
self.__topics_subscribed = False
Expand Down Expand Up @@ -162,6 +164,8 @@ def unmount(self):
self.log.info("Stop requested !")
# Kill alive flag of the worker class
self.alive = False
# Cancels tasks
self.cancel_all_tasks()

# ---

Expand Down Expand Up @@ -266,7 +270,7 @@ async def __drv_state_connecting(self):
async def __drv_state_init(self):
"""
"""
self.platform.load_task(self.__alive_task())
self.load_interface_task(self.__alive_task())
self.__subscribe_topics()
await self._PZA_DRV_loop_init()

Expand Down Expand Up @@ -544,36 +548,26 @@ async def _PZA_DRV_cmds_set(self, payload):



###########################################################################
###########################################################################

# def hunt(self):
# """
# """
# config = self._PZA_DRV_config()
# name = "unnamed" if "name" not in config else config["name"]
# description = "" if "description" not in config else config["description"]
# template = self._PZA_DRV_tree_template()
# driver = {
# "name": name,
# "description": description,
# "template": template
# }
# meat = self._PZA_DRV_hunt_instances()
# instances = None
# if meat:
# instances = {
# "name": name,
# "instances": meat
# }
# return driver, instances


# =============================================================================
# PROTECTED FUNCTIONS

# ---

def load_interface_task(self, coro, name = None):
if name == None:
name=f"FROM>{self.PZA_WORKER_name()}"
new_task = self.platform.load_task(coro, name)
self._tasks.append(new_task)
return new_task

# =============================================================================
# PROTECTED FUNCTIONS
# ---

def cancel_all_tasks(self):
for t in self._tasks:
t.cancel()

# ---

Expand Down
25 changes: 23 additions & 2 deletions platform/panduza_platform/devices/hanmatek/hm310t/dev_hm310t.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,34 @@ def _PZA_DEV_config(self):
"""
"""
return {
"family": "PSU",
"family": "BPS",
"model": "Hm310t",
"manufacturer": "Hanmatek"
"manufacturer": "Hanmatek",
"settings_props": [
{
'name': 'serial_port_name',
'type': 'string',
'default': ''
}
]
}

# ---

async def _PZA_DEV_hunt(self):
"""
"""
# print( HuntUsbDevs('0416', '5011', 'tty') )

# connector = await SerialTty.Get(serial_port_name='/dev/ttyACM0')
# status = await connector.write_and_read_until("*IDN?", time_lock_s=0.5)

# print(">>> ", status)

return []

# ---

async def _PZA_DEV_mount_interfaces(self):
"""
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async def _PZA_DRV_loop_init(self):
}

# Append a task to refresh platform data
self.platform.load_task(self.__refresh_platform_data_task())
self.load_interface_task(self.__refresh_platform_data_task())


# status
Expand Down
6 changes: 3 additions & 3 deletions platform/panduza_platform/meta_drivers/bpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ async def _PZA_DRV_loop_init(self):
}

# Start polling task
self.platform.load_task(self.__polling_task_att_enable())
self.platform.load_task(self.__polling_task_att_voltage())
self.platform.load_task(self.__polling_task_att_current())
self.load_interface_task(self.__polling_task_att_enable())
self.load_interface_task(self.__polling_task_att_voltage())
self.load_interface_task(self.__polling_task_att_current())

# Init success, the driver can pass into the run mode
self._PZA_DRV_init_success()
Expand Down
12 changes: 9 additions & 3 deletions platform/tests/manual/platform_basics.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import time
import numpy as np
from panduza import Core, Platform
from panduza import Client, Core, Platform

ADDR="localhost"
PORT=1883

Core.EnableLogging()

# plat = Platform(addr=ADDR, port=PORT, topic="pza/server/devbox/test")
plat = Platform(addr=ADDR, port=PORT, topic="pza/server/vm/test")
c = Client(url=ADDR, port=PORT)
c.connect()
platforms = c.scan_all_platform_interfaces()

platform_topic = next(iter(platforms.keys()))
print(platform_topic)

plat = Platform(addr=ADDR, port=PORT, topic=platform_topic)


plat.dtree.content.set({
Expand Down
12 changes: 9 additions & 3 deletions platform/tests/manual/platform_reset.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import time
import numpy as np
from panduza import Core, Platform
from panduza import Client, Core, Platform

ADDR="localhost"
PORT=1883

Core.EnableLogging()

# plat = Platform(addr=ADDR, port=PORT, topic="pza/server/devbox/test")
plat = Platform(addr=ADDR, port=PORT, topic="pza/server/vm/test")
c = Client(url=ADDR, port=PORT)
c.connect()
platforms = c.scan_all_platform_interfaces()

platform_topic = next(iter(platforms.keys()))
print(platform_topic)

plat = Platform(addr=ADDR, port=PORT, topic=platform_topic)

plat.dtree.content.set({
})
Expand Down

0 comments on commit dae126c

Please sign in to comment.