Search code examples
pythonscala

How to make an asynchronous python call from within scala


I am trying to access the Python Client V4 for DyDx from within a scala project. I integrated the previous V3 using scalapy library. But V4 contains asynchronous calls which I don't know how I should handle. So for example the short term order composite example's test function begins with the following lines:

node = await NodeClient.connect(TESTNET.node)
indexer = IndexerClient(TESTNET.rest_indexer)

I can access the connect call as follows in my scala code:

    val network = py.module("dydx_v4_client.network")
    val client = py.module("dydx_v4_client.node.client")

If I print out the value for client it gives me

<coroutine object NodeClient.connect at 0x7fe18e87a340>

The connect call in NodeClient is defined as asynchronous:

async def connect(config: NodeConfig) -> Self:
...

How can I actually execute this coroutine object?


Solution

  • A solution I found is to use the asyncio.run function which can be used to run coroutine:

        val asyncio = py.module("asyncio")
        val node = asyncio.run(client.NodeClient.connect(network.TESTNET.node))