Skip to content

Commit 7a32999

Browse files
committed
Updated examples
1 parent fcbcfcf commit 7a32999

File tree

4 files changed

+21
-30
lines changed

4 files changed

+21
-30
lines changed
Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,30 @@
11
# Import the basic framework components.
2-
from softioc import softioc, builder
2+
from softioc import softioc, builder, asyncio_dispatcher
33
from aioca import caget, caput
44
import asyncio
55

6+
# Create an asyncio dispatcher, the event loop is now running
7+
dispatcher = asyncio_dispatcher.AsyncioDispatcher()
8+
69
# Set the record prefix
710
builder.SetDeviceName("MY-DEVICE-PREFIX")
811

912
# Create some records
1013
ai = builder.aIn('AI', initial_value=5)
11-
12-
def update(val):
13-
print("got here")
14-
print(val)
15-
ai.set(val)
16-
17-
ao = builder.aOut('AO', initial_value=12.45, on_update=update)
14+
ao = builder.aOut('AO', initial_value=12.45, always_update=True,
15+
on_update=lambda v: ai.set(v))
1816

1917
# Boilerplate get the IOC started
2018
builder.LoadDatabase()
21-
softioc.iocInit()
22-
23-
# Perform some reading/writing to the PVs
24-
async def do_read_write():
25-
print(await caget("MY-DEVICE-PREFIX:AO"))
26-
print(await caget("MY-DEVICE-PREFIX:AI"))
27-
await caput("MY-DEVICE-PREFIX:AO", "999")
28-
await asyncio.sleep(10)
29-
print(await caget("MY-DEVICE-PREFIX:AI"))
30-
print(await caget("MY-DEVICE-PREFIX:AO"))
31-
32-
#asyncio.run(do_read_write())
33-
34-
print(asyncio.run(caget("MY-DEVICE-PREFIX:AI")))
35-
print(asyncio.run(caget("MY-DEVICE-PREFIX:AO")))
36-
print(asyncio.run(caput("MY-DEVICE-PREFIX:AO","999")))
19+
softioc.iocInit(dispatcher)
3720

38-
print(asyncio.run(caget("MY-DEVICE-PREFIX:AI")))
39-
print(asyncio.run(caget("MY-DEVICE-PREFIX:AO")))
21+
# Start processes required to be run after iocInit
22+
async def update():
23+
while True:
24+
ai.set(ai.get() + 1)
25+
await asyncio.sleep(1)
4026

27+
asyncio.run_coroutine_threadsafe(update(), dispatcher.loop)
4128

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

docs/examples/example_cothread_ioc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
# Create some records
99
ai = builder.aIn('AI', initial_value=5)
10-
ao = builder.aOut('AO', initial_value=12.45, on_update=lambda v: ai.set(v))
10+
ao = builder.aOut('AO', initial_value=12.45, always_update=True,
11+
on_update=lambda v: ai.set(v))
1112

1213
# Boilerplate get the IOC started
1314
builder.LoadDatabase()
@@ -19,7 +20,6 @@ def update():
1920
ai.set(ai.get() + 1)
2021
cothread.Sleep(1)
2122

22-
2323
cothread.Spawn(update)
2424

2525
# Finally leave the IOC running with an interactive shell.

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extend-ignore =
4040

4141
[tool:pytest]
4242
# Run pytest with all our checkers, and don't spam us with massive tracebacks on error
43-
addopts = --tb=native -vv --doctest-modules --ignore=iocStats --ignore=epicscorelibs
43+
addopts = --tb=native -vv --doctest-modules --ignore=iocStats --ignore=epicscorelibs --ignore=docs
4444

4545
[coverage:run]
4646
# This is covered in the versiongit test suite so exclude it here

tests/test_asyncio.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ def asyncio_ioc():
3434
async def test_asyncio_ioc(asyncio_ioc):
3535
import asyncio
3636
from aioca import caget, caput, camonitor, CANothing, _catools
37-
# Unregister the atexit handler as it conflicts with cothread
37+
# Unregister the aioca atexit handler as it conflicts with the one installed
38+
# by cothread. If we don't do this we get a seg fault. This is not a problem
39+
# in production as we won't mix aioca and cothread, but we do mix them in
40+
# the tests so need to do this.
3841
atexit.unregister(_catools._catools_atexit)
3942

4043
# Start

0 commit comments

Comments
 (0)