DiamondLightSource / pythonSoftIOC

Embed an EPICS IOC in a Python process
Apache License 2.0
31 stars 9 forks source link

Add a non-interactive function to use softioc in deamon mode #155

Closed XavSPM closed 5 months ago

XavSPM commented 5 months ago

I propose adding the non_interactive_ioc function to allow the use of softioc in daemon mode. This mode can, for example, be used with systemd. It is compatible with asyncio and cothread.

To ensure that information is correctly logged in the journal, it is recommended to execute the Python script with the -u option or to set the environment variable PYTHONUNBUFFERED=TRUE.

Here is an example of a service:

[Unit]
Description=IOC
After=network.target

[Service]
User=root
Group=root
WorkingDirectory=/opt
ExecStart=/opt/softioc/bin/python -u /opt/example.py
Environment="PATH=/opt/softioc/bin"
Restart=always

[Install]
WantedBy=multi-user.target

And an example of a script:

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

# Set the record prefix
builder.SetDeviceName("MY-DEVICE-PREFIX")

# Create some records
ai = builder.aIn('AI', initial_value=5)
ao = builder.aOut('AO', initial_value=12.45, on_update=lambda v: ai.set(v))

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

# Start processes required to be run after iocInit
def update():
    while True:
        ai.set(ai.get() + 1)
        print(ai.get())
        cothread.Sleep(1)

cothread.Spawn(update)

if __name__ == "__main__":
    softioc.non_interactive_ioc()
AlexanderWells-diamond commented 5 months ago

Apologies, there's another PR under discussion at the moment that will significantly change the AsyncioDispatcher: https://github.com/dls-controls/pythonSoftIOC/pull/138. It would probably be best to wait until that one is merged before thinking about this one, as it will require refactoring.

XavSPM commented 5 months ago

Noted. I'm waiting for merger #138.

Do you think there will be many changes to make following the merger?

AlexanderWells-diamond commented 5 months ago

The other PR has now been merged. There is a conflict that will need fixing.

XavSPM commented 5 months ago

Hello, @AlexanderWells-diamond

I created another pull request (#156) following merger #138