dls-controls / pythonSoftIOC

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

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

Closed XavSPM closed 2 months ago

XavSPM commented 2 months ago

I propose this modification, following the initial request #155

Reminder of the initial proposal:

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()
XavSPM commented 2 months ago

Hello, @AlexanderWells-diamond

I've made the requested changes and also reduced the number of commits.

Is there anything else I can do ?

Araneidae commented 2 months ago

I have squashed this work into a single commit, fixed a couple of trivial whitespace issues, and pushed to master. This is now accepted (the "conflicts" are just my whitespace fixes), so will close this PR as completed.

Thank you for this.