Пример #1
0
def test_curio_server_example(prefix, run_client):
    import caproto.curio.client as client
    from caproto.ioc_examples.type_varieties import (pvdb)
    from caproto.curio.server import ServerExit, start_server as server_main

    pvdb = {prefix + key: value for key, value in pvdb.items()}
    pi_pv = prefix + 'int'
    broadcaster = client.SharedBroadcaster()
    ctx = client.Context(broadcaster)

    async def connect():
        await broadcaster.register()
        await ctx.search(pi_pv)
        print('done searching')
        chan1 = await ctx.create_channel(pi_pv)
        # ...and then wait for all the responses.
        await chan1.wait_for_connection()
        return chan1

    async def task():
        async def server_wrapper():
            try:
                await server_main(pvdb)
            except ServerExit:
                print('Server exited normally')

        try:
            server_task = await curio.spawn(server_wrapper)
            await curio.sleep(1)  # Give server some time to start up.
            chan1 = await connect()
            await run_client(chan1, pvdb, ctx)
            await chan1.disconnect()
            print('client is done')
        finally:
            try:
                await server_task.cancel()
                await server_task.join()
            except curio.KernelExit:
                print('Server exited normally')

    with curio.Kernel() as kernel:
        kernel.run(task)
    print('done')
Пример #2
0
def _test_ioc_examples(request,
                       module_name,
                       pvdb_class_name,
                       class_kwargs,
                       prefix,
                       async_lib='curio'):
    from .conftest import run_example_ioc
    from caproto.sync.client import read, write
    from caproto.server import PvpropertyReadOnlyData
    import subprocess

    module = __import__(module_name,
                        fromlist=(module_name.rsplit('.', 1)[-1], ))

    pvdb_class = getattr(module, pvdb_class_name)

    print(f'Prefix: {prefix} PVDB class: {pvdb_class}')
    pvdb = pvdb_class(prefix=prefix, **class_kwargs).pvdb
    pvs = list(pvdb.keys())
    pv_to_check = pvs[0]

    print(f'PVs:', pvs)
    print(f'PV to check: {pv_to_check}')

    stdin = (subprocess.DEVNULL if 'io_interrupt' in module_name else None)

    print('stdin=', stdin)
    run_example_ioc(module_name,
                    request=request,
                    args=['--prefix', prefix, '--async-lib', async_lib],
                    pv_to_check=pv_to_check,
                    stdin=stdin)

    print(f'{module_name} IOC now running')

    put_values = [
        (PvpropertyReadOnlyData, None),
        (ca.ChannelNumeric, [1]),
        (ca.ChannelString, ['USD']),
    ]

    skip_pvs = [('ophyd', ':exit')]

    def find_put_value(pv):
        'Determine value to write to pv'
        for skip_ioc, skip_suffix in skip_pvs:
            if skip_ioc in module_name:
                if pv.endswith(skip_suffix):
                    return None

        for put_class, put_value in put_values:
            if isinstance(channeldata, put_class):
                return put_value
        else:
            raise Exception('Failed to set default value for channeldata:'
                            f'{channeldata.__class__}')

    for pv, channeldata in pvdb.items():
        value = find_put_value(pv)
        if value is None:
            print(f'Skipping write to {pv}')
            continue

        print(f'Writing {value} to {pv}')
        write(pv, value)

        value = read(pv)
        print(f'Read {pv} = {value}')