Run component without monopolizing the event loop

I need to run a WAMP client component within a webserver (FastAPI).

All examples use the ‘run’ method to launch the component. However the run method will monopolize the event-loop as it is going to call ‘loop.run_forever’.

So currently I’m calling the ‘_run’ underneath directly from within a task but is that the proper way of doing it?

toon

You’re using the Component API, right? In that case, there is the start() method, which is intended for your use-case. The run() method starts logging, start()s all the components, handles cleanup, etc … so if you’re already handling all that, the start() method should be the right approach.

Thanks.

However I tried the below for instance and I get an error in aio.py:473> attached to a different loop

import asyncio
from autobahn.asyncio.component import Component

async def amain():
    def joined(session, details):
        print("session ready")

    comp = Component(transports='ws://mydomain.net/crossbario/ws')
    comp.on('join', joined)

    fut = comp.start()
   await fut

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

Seems that the issue I reported above is solved by putting the import statements inside the amain function

This should fix the loop problems: https://github.com/crossbario/txaio/pull/163
Will be in the next release.

1 Like

… and here you are:

1 Like