xenanetworks / open-automation-python-api

🚀 XOA object-oriented Python API library for traffic generation and network emulation.
https://docs.xenanetworks.com/projects/xoa-python-api
Apache License 2.0
7 stars 4 forks source link

After connection establish the code hangs #70

Closed leonardhyu closed 1 year ago

leonardhyu commented 1 year ago

Reported by @hosseingbi. The code hangs after await establish_connection(handler1, "10.20.10.212"). It happens sporadically.

import asyncio
# import available module
from xoa_driver import utils
from xoa_driver.lli import commands as cmd
from xoa_driver.lli import TransportationHandler
from xoa_driver.lli import establish_connection
from internal_core import local_commands as localcmd
from internal_core import local_types as localtype
import time

async def my_awesome_script():
    handler1 = TransportationHandler(debug=False)
    await establish_connection(handler1, "10.20.10.212")
    await utils.apply(
        cmd.C_LOGON(handler1).set("xena"),
        cmd.C_OWNER(handler1).set("hnaxoaddddd"),
        cmd.P_RESERVATION(handler1,3,0).set(operation=cmd.ReservedAction.RESERVE),
        localcmd.P4_DHCP_CONFIG(handler1, 3, 0).set(10, 3, 5000, "0a:0a:0a:00:00:00", cmd.IsEnabled.DISABLE, 0, cmd.IsEnabled.DISABLE, 0)
    ) # establish connection using username "JonDoe".

...

def main():
    try:
        loop = asyncio.get_event_loop()
        loop.create_task(my_awesome_script())
        loop.run_forever()
    except KeyboardInterrupt:
        pass
if __name__ == "__main__":
    main()
leonardhyu commented 1 year ago

@ArtemConstantinov I've googled a bit on this. Do you think the following would work? https://stackoverflow.com/questions/34470856/event-loop-created-by-asyncio-new-event-loop-hangs https://github.com/rethinkdb/rethinkdb-python/issues/216

The issue happened again today, but when we ran the following, it behaved normally.

>>> import asyncio
>>> asyncio.run(asyncio.sleep(1))

I've advised @hosseingbi to switch his Python version to 3.10 and back to 3.9 again when the hanging happens again.

ArtemConstantinov commented 1 year ago

please try to change the code from:

def main():
    try:
        loop = asyncio.get_event_loop()
        loop.create_task(my_awesome_script())
        loop.run_forever()
    except KeyboardInterrupt:
        pass
if __name__ == "__main__":
    main()

to

def main():
    try:
        loop = asyncio.new_event_loop() # science python 3.10 for older version can call asyncio.get_event_loop()
        loop.run_until_complete(my_awesome_script())
    except KeyboardInterrupt:
        pass
if __name__ == "__main__":
    main()

or

```python
def main():
    try:
        asyncio.run(my_awesome_script())
    except KeyboardInterrupt:
        pass
if __name__ == "__main__":
    main()
ArtemConstantinov commented 1 year ago

that behavior is improved in the driver of version 2, it will raise exception if connection won't established before the timeout will be expired