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

RuntimeError when using along with any async code #70

Closed
DNikolaevAtRocket opened this issue Jun 9, 2023 · 2 comments
Closed

RuntimeError when using along with any async code #70

DNikolaevAtRocket opened this issue Jun 9, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@DNikolaevAtRocket
Copy link

Description

tnz fails with RuntimeError: This event loop is already running as soon as there's an event loop used by any other code.

How to reproduce

Just try to call Tnz.wait in any combination with async code. See the simple example below.

from tnz.tnz import Tnz
import asyncio


async def main():
    tnz = Tnz()
    tnz.connect('<HOSTNAME>', 23)
    tnz.wait()
    print(tnz.scrstr(rstrip=True))
    tnz.close()

asyncio.run(main())
@najohnsn
Copy link
Member

najohnsn commented Jun 9, 2023

Under the covers, these functions manage an async event loop. Functions that are suitable for use in an async function is a work in progress. I'm glad to hear there is interest.

@najohnsn najohnsn added the enhancement New feature or request label Jun 9, 2023
@najohnsn
Copy link
Member

najohnsn commented Jul 5, 2023

With #71, your example program can be changed to what follows to use with asyncio:

from tnz.tnz import Tnz
import asyncio


async def main():
    event = asyncio.Event()
    tnz = Tnz()
    tnz.connect("s390vm", event=event)
    await event.wait()
    event.clear()  # set up to re-use event
    print(tnz.scrstr(rstrip=True))
    tnz.close()

asyncio.run(main())

Note that tnz will call event.set() for every update from the server. To re-use it, use event.clear() after the await as noted above.

Note that Tnz.wait() cannot be used for this use case. And Tnz provides low-level APIs while Ati provides high-level APIs. But there are not yet any high-level APIs that can be used in this manner (in an asyncio event loop).

@najohnsn najohnsn closed this as completed Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants