JuliaPy / PythonCall.jl

Python and Julia in harmony.
https://juliapy.github.io/PythonCall.jl/stable/
MIT License
776 stars 63 forks source link

How to await a python coroutine in julia #320

Closed bryaan closed 1 year ago

bryaan commented 1 year ago

How do you await a coroutine generated by python in julia using PythonCall.jl?

This would seem to work for a future. But what about coroutines?

function wait_on_py(future)
    while !Bool(future.done())
        sleep(0.1)
    end
    future
end

future |> wait_on_py
cjdoris commented 1 year ago

asyncio.run(coro)? https://docs.python.org/3/library/asyncio-runner.html#asyncio.run

bryaan commented 1 year ago

It works in python but when calling it from julia im getting an error.

import asyncio 

async def create_filter_coro(web3, fromBlock, topics):
    eventFilter = await web3.eth.filter({
            "fromBlock": fromBlock,
            "topics": topics
        })
    return eventFilter

def create_filter_async(web3, fromBlock, topics):
    return asyncio.run(create_filter_coro(web3, fromBlock, topics))
web3 = web3.AsyncWeb3(web3.AsyncHTTPProvider(jsonRpcUrl))
eventFilter = utils.create_filter_async(web3, 17418534, ["xxx"])
┌ Error: 2023-06-05 23:44:06 Unexpected PyException:
│   exception =
│    Python: TypeError: object function can't be used in 'await' expression
│    Python stacktrace:
│     [1] construct_middleware
│       @ web3.middleware ~/Library/Caches/pypoetry/virtualenvs/cryptopy-5siZoxZ4-py3.10/lib/python3.10/site-packages/web3/middleware/__init__.py:130
│     [2] async_combine_middlewares
│       @ web3.middleware ~/Library/Caches/pypoetry/virtualenvs/cryptopy-5siZoxZ4-py3.10/lib/python3.10/site-packages/web3/middleware/__init__.py:119
bryaan commented 1 year ago

Again in python alone the code works. So it must be something with this lib and async functions or types.

The call stack points to this line: https://github.com/ethereum/web3.py/blame/c99135587614c4f95275ea27dd3227f45ac23c73/web3/middleware/__init__.py#L130

cjdoris commented 1 year ago

Can you provide a full MWE of how to get to the error, and include the full error output too please.

bryaan commented 1 year ago

I'm going to leave this as is for now since this library doesn't really support threading.