FreeOpcUa/opcua-asyncio

Could not load shelf file on server init if it already exists

varioboticHolger opened this issue · 0 comments

Describe the bug

Initializing a server with server.init(Path('cache_file')) will create a cache_file.db in the same directory as the script has been executed. Running the script the second time will recreate the same cache file.

Solution

In the internal_server.py file the load_standard_address_space function must be modified, that the Path(...).with_suffix('.db') can be recognized.

Current code snippet:

async def load_standard_address_space(self, shelf_file: Optional[Path] = None):
    if shelf_file:
            is_file = await asyncio.get_running_loop().run_in_executor(
                None, Path.is_file, shelf_file
            ) or await asyncio.get_running_loop().run_in_executor(None, Path.is_file, shelf_file.with_suffix('.db'))

Working code snippet:

async def load_standard_address_space(self, shelf_file: Optional[Path] = None):
        if shelf_file:
            is_file = await asyncio.get_running_loop().run_in_executor(
                None, Path.is_file, shelf_file
            ) or await asyncio.get_running_loop().run_in_executor(None, Path.is_file, shelf_file.with_suffix('.db'))

To Reproduce

Steps to reproduce the behavior incl code:
Calling this script:

import asyncio
from pyinstrument import Profiler
from asyncua import Server
from pathlib import Path

async def opcua_startup():
    server = Server()
    await server.init(Path('cache_file'))
    print("Server initialized")
    server.set_endpoint('opc.tcp://localhost:4840/freeopcua/server/')
    server.set_server_name('FreeOpcUa Example Server')
    await server.start()

    print("Server started")

    return True


async def main():
    p = Profiler()
    with p:
        await opcua_startup()
    p.print()

if __name__ == '__main__':
    asyncio.run(main())

Expected behavior

A clear and concise description of what you expected to happen.
If this script would be called twice. The cache file should be catched and used to be much more faster

Version

Python-Version:3.9

opcua-asyncio Version: 1.1.5
Hardware: RevolutionPi / RaspberryPi