Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing a custom asyncio event loop into the AsyncioDispatcher causes methods to never run #95

Closed
AlexanderWells-diamond opened this issue Jul 26, 2022 · 0 comments · Fixed by #96
Labels

Comments

@AlexanderWells-diamond
Copy link
Collaborator

Passing a custom loop to the AsyncioDispatcher causes no on_update methods to be called. This can be seen in this code by running it and running caput ABC:AO 1, and observing the lack of output. Uncomment the second event_loop definition to see it working.

# Import the basic framework components.
from softioc import softioc, builder, asyncio_dispatcher
import asyncio

# Create an asyncio dispatcher, the event loop is now running
event_loop = asyncio.get_event_loop()
# event_loop = None
dispatcher = asyncio_dispatcher.AsyncioDispatcher(event_loop)

# Set the record prefix
builder.SetDeviceName("ABC")

# Create some records
ao = builder.aOut('AO', initial_value=12.45, always_update=True,
                  on_update=print)

# Boilerplate get the IOC started
builder.LoadDatabase()
softioc.iocInit(dispatcher)

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

This is caused by the event loop that is retrieved by asyncio.get_event_loop() not running anywhere.

If there's no event loop, we create our own and set it running forever in its own newly created thread. We need to implement something similar to this for passed in event loops - possibly use loop.is_running() to know if its already running somewhere else, and if not we create a new thread for it to run in.

We should also take this time to document the AsyncioDispatcher, which currently has no explicit documentation - there's one line about the module it's in but nothing about the class itself, nor any limitations on what loop we can pass in.

(An alternative solution to this is to simply remove the ability to pass in custom event loops. However, as we have released versions with this API that's probably not desirable)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant