Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync.Clinet is not reusable #1364

Open
okapies opened this issue Jul 18, 2023 · 5 comments
Open

sync.Clinet is not reusable #1364

okapies opened this issue Jul 18, 2023 · 5 comments

Comments

@okapies
Copy link
Contributor

okapies commented Jul 18, 2023

Describe the bug
Calling connect() on sync.Client after disconnect() will raise ThreadLoopNotRunning error.

To Reproduce

>>> client = Client('opc.tcp://localhost')
>>> client.connect()
>>> client.disconnect()
>>> client.connect()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/project/venv/lib/python3.10/site-packages/asyncua/sync.py", line 95, in wrapper
    result = self.tloop.post(aio_func(*args, **kwargs))
  File "/home/user/project/venv/lib/python3.10/site-packages/asyncua/sync.py", line 51, in post
    raise ThreadLoopNotRunning(f"could not post {coro}")
asyncua.sync.ThreadLoopNotRunning: could not post <coroutine object Client.connect at 0x7faf0b51ace0>

Expected behavior
(Re-)initialize tloop every time we call connect().

Version

  • Python-Version: 3.10
  • opcua-asyncio Version (e.g. master branch, 0.9): 1.0.2
@oroulet
Copy link
Member

oroulet commented Jul 18, 2023

This is really common. You cannot restart a Thread object. But the error message is is really hard to understand...we need to improve that..

@okapies
Copy link
Contributor Author

okapies commented Jul 18, 2023

Yes, but the async Client is reusable instead. This is problematic especially when want to combine both versions. Can we fix to start the thread when connect instead of __init__?

@miki5799
Copy link

miki5799 commented Nov 6, 2023

Same is happening to me and I agree with your proposed expected behaviour.

@aclementptw
Copy link

I'd like to bump this, as we are currently migrating from opcua to opcua-asyncio using the wrapper functions.
We do reuse Clients and this poses a problem for us.

miki5799 added a commit to miki5799/opcua-asyncio that referenced this issue Nov 23, 2023
Reopen ThreadLoop if  stopped previously analogous to init
Fixes FreeOpcUa#1364
@miki5799
Copy link

miki5799 commented Nov 23, 2023

Yes, but the async Client is reusable instead. This is problematic especially when want to combine both versions. Can we fix to start the thread when connect instead of __init__?

Hello everyone,

I have submitted a PR #1503, in which a new thread is started analogous to __init__ iff the ThreadLoop has stopped, so that the updated connect method looks like this:

def connect(self) -> None:
    if not self.tloop.is_alive():
        self.tloop = ThreadLoop()
        self.tloop.start()
        self.close_tloop = True
    self.tloop.post(self.aio_obj.connect())

Analogous for the Server's start method.

This now ensures reusability of client and server, allowing a smoother migration, as mentioned by @aclementptw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants