簡體   English   中英

如何在 web3py 中使用 AsyncHTTPProvider?

[英]How to use AsyncHTTPProvider in web3py?

我正在嘗試在項目中使用 AsyncHTTPProvider,這是我的測試代碼:

import asyncio
from web3 import Web3
import asyncio

async def main():
    w3 = Web3(Web3.AsyncHTTPProvider("http://127.0.0.1:8545"))
    coinbase = await w3.eth.coinbase
    print(coinbase)

if __name__ == "__main__":
    asyncio.run(main())

我將 ganache-cli 用於本地提供程序。

我在網上找不到任何使用 AsyncHTTPProvider 的人的例子,即使在 web3py github 中我也找不到我能理解的例子(閱讀大型庫的拉取請求非常乏味)

當我運行此代碼時,出現以下錯誤:

Traceback (most recent call last):
  File "test.py", line 11, in <module>
    asyncio.run(main())
  File "C:\Program Files\Python37\lib\asyncio\runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "C:\Program Files\Python37\lib\asyncio\base_events.py", line 587, in run_until_complete
    return future.result()
  File "test.py", line 7, in main
    coinbase = await w3.eth.coinbase
  File "D:\Name\Ethereum\FirstProject\env\lib\site-packages\web3\eth.py", line 282, in coinbase
    return self.get_coinbase()
  File "D:\Name\Ethereum\ArbitrageMax\env\lib\site-packages\web3\module.py", line 57, in caller
    result = w3.manager.request_blocking(method_str, params, error_formatters)
  File "D:\Name\Ethereum\FirstProject\env\lib\site-packages\web3\manager.py", line 154, in request_blocking
    response = self._make_request(method, params)
  File "D:\Name\Ethereum\FirstProject\env\lib\site-packages\web3\manager.py", line 133, in _make_request
    return request_func(method, params)
TypeError: 'coroutine' object is not callable
sys:1: RuntimeWarning: coroutine 'AsyncBaseProvider.request_func' was never awaited

查看錯誤日志,盡管使用了 AsyncHTTPProvider,但仍在使用 'w3.manager.request_blocking',這表明它仍在使用同步 api

我運行以下代碼以確保 web3 對象的提供程序實際上是異步的:

import asyncio
from web3 import Web3
import asyncio

async def main():
    w3 = Web3(Web3.AsyncHTTPProvider("http://127.0.0.1:8545"))
    print(type(w3.provider))
    # coinbase = await w3.eth.coinbase
    # print(coinbase)

if __name__ == "__main__":
    asyncio.run(main())

輸出:

<class 'web3.providers.async_rpc.AsyncHTTPProvider'>

查看 web3py 存儲庫的測試,似乎我做的一切都正確,因為測試與 w3.eth 異步 api 交互的方式與我嘗試的方式相同:

class AsyncEthModuleTest:    
    ... # other tests

    # test of get balance which involves awaiting coinbase
    @pytest.mark.asyncio
    async def test_eth_get_balance(self, async_w3: "Web3") -> None:
        coinbase = await async_w3.eth.coinbase  # type: ignore

        with pytest.raises(InvalidAddress):
            await async_w3.eth.get_balance(  # type: ignore
                ChecksumAddress(HexAddress(HexStr(coinbase.lower())))
            )

        balance = await async_w3.eth.get_balance(coinbase)  # type: ignore

        assert is_integer(balance)
        assert balance >= 0

添加測試的拉取請求的鏈接(問題 #2056)

唯一的問題是我無法弄清楚“async_w3”對象的聲明在哪里

我開始我的項目的 web3 版本是: web3==5.20.1 我更新到最新版本,看看它是否能解決問題(web3==5.23.0),不,仍然是同樣的舊錯誤

我在 web3 github 中得到了答案

這是創建提供程序的正確語法(不過我還沒有測試過)

from web3 import Web3
from web3.eth import AsyncEth

w3 = Web3(Web3.AsyncHTTPProvider("http://127.0.0.1:8545"), modules={'eth': (AsyncEth,)}, middlewares=[])

如果將來有人發現這個問題:請記住,到目前為止,異步是一項正在進行的工作。 將來可能會發生變化,您需要檢查文檔

支持的中間件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM