dls-controls / pythonSoftIOC

Embed an EPICS IOC in a Python process
Apache License 2.0
32 stars 7 forks source link

Update asyncio example to show exceptions in long running functions being tracked #94

Closed coretl closed 1 year ago

coretl commented 2 years ago

This is a failing of the example code. We put some exception handling in the dispatcher, so if an function passed to on_update fails we get the traceback printed, but we don't do anything with the long running update function in the example. We can make use of the same exception handling by scheduling it with the dispatcher:

...
# Start processes required to be run after iocInit
async def update():
    while True:
        ai.set(ai.get() + 1)
        await asyncio.sleep(1)
        raise Exception("Boom")

dispatcher(update)

# Finally leave the IOC running with an interactive shell.
softioc.interactive_ioc(globals())

This now prints

Starting iocInit
############################################################################
## EPICS 7.0.6.1
## Rev. 7.0.6.99.2.0
############################################################################
iocRun: All initialization complete
Python 3.7.2 (default, Jan 20 2020, 11:03:41) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> ERROR:root:Exception when awaiting callback
Traceback (most recent call last):
  File "/dls/science/users/tmc43/common/python/pythonSoftIOC/softioc/asyncio_dispatcher.py", line 35, in async_wrapper
    await ret
  File "example_asyncio_ioc.py", line 26, in update
    raise Exception("Boom")
Exception: Boom

The logging message isn't the best though, I'll make an issue to track this

Originally posted by @coretl in https://github.com/dls-controls/pythonSoftIOC/discussions/93#discussioncomment-2991914