首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >isConnected()冻结并在web3.py中不返回任何内容

isConnected()冻结并在web3.py中不返回任何内容
EN

Ethereum用户
提问于 2021-06-07 17:06:51
回答 1查看 167关注 0票数 1

我用:

python3.7

web3==4.9.2

Infura和/或own Node

初始化web3后,它只在第一次返回isConnected()响应,然后冻结。

web3.py:

代码语言:javascript
运行
复制
from web3 import Web3
from web3.gas_strategies.rpc import rpc_gas_price_strategy
from web3.providers.auto import load_provider_from_uri  


class Web3NotAvailableException(Exception):
    def __init__(self, rpc_url):
        self.rpc_url = rpc_url

    def __str__(self):
        return f'WEB3_RPC_URL: {self.rpc_url}'


def get_web3():
    logger.debug('Getting web3 instance')
    if hasattr(get_web3, '_web3_instance'):
        logger.warn('Here 1')
        _web3_instance = getattr(get_web3, '_web3_instance')
    else:
        logger.warn('Here 2')
        _web3_instance = Web3(load_provider_from_uri(settings.WEB3_RPC_URL))
        _web3_instance.eth.setGasPriceStrategy(rpc_gas_price_strategy)
        setattr(get_web3, '_web3_instance', _web3_instance)

    logger.warn('Before isConnected()')
    if not _web3_instance.isConnected():
        logger.warn('Here 3')
        _web3_instance = None
        raise Web3NotAvailableException(settings.WEB3_RPC_URL)

    logger.warn('Finish')
    return _web3_instance

日志:

代码语言:javascript
运行
复制
DEBUG 07/06/2021 16:30:13 | [ web3.py:46 ] Getting web3 instance
WARNING 07/06/2021 16:30:13 | [ web3.py:51 ] Here 2
WARNING 07/06/2021 16:30:13 | [ web3.py:56 ] Before isConnected()
WARNING 07/06/2021 16:30:13 | [ web3.py:62 ] Finish
DEBUG 07/06/2021 16:30:15 | [ web3.py:46 ] Getting web3 instance
WARNING 07/06/2021 16:30:15 | [ web3.py:48 ] Here 1
WARNING 07/06/2021 16:30:15 | [ web3.py:56 ] Before isConnected()

在那个码头集装箱结冰后什么也没发生。尽管celery应该每30秒推一次任务。

但是,如果我在本地使用ganache,一切都会正常工作。

代码语言:javascript
运行
复制
DEBUG 07/06/2021 16:59:37 | [ web3.py:46 ] Getting web3 instance
WARNING 07/06/2021 16:59:37 | [ web3.py:51 ] Here 2
WARNING 07/06/2021 16:59:37 | [ web3.py:56 ] Before isConnected()
WARNING 07/06/2021 16:59:37 | [ web3.py:62 ] Finish
################################################
DEBUG 07/06/2021 17:00:11 | [ web3.py:46 ] Getting web3 instance
WARNING 07/06/2021 17:00:11 | [ web3.py:48 ] Here 1
WARNING 07/06/2021 17:00:11 | [ web3.py:56 ] Before isConnected()
WARNING 07/06/2021 17:00:11 | [ web3.py:62 ] Finish
################################################
DEBUG 07/06/2021 17:00:41 | [ web3.py:46 ] Getting web3 instance
WARNING 07/06/2021 17:00:41 | [ web3.py:48 ] Here 1
WARNING 07/06/2021 17:00:41 | [ web3.py:56 ] Before isConnected()
WARNING 07/06/2021 17:00:41 | [ web3.py:62 ] Finish

怎么搞错了?

EN

回答 1

Ethereum用户

回答已采纳

发布于 2021-06-07 20:29:01

我将web3初始化移到了设置文件中,现在它只初始化了一次,我已经获得了Celery的web3-结果:

设置/生产.production.py:

代码语言:javascript
运行
复制
from web3 import Web3
from web3.gas_strategies.rpc import rpc_gas_price_strategy
from web3.providers.auto import load_provider_from_uri
from django.conf import settings

from eth_watcher.web3 import Web3NotAvailableException


##################
# different variables here
##################
 
def get_web3():
    _web3_instance = Web3(load_provider_from_uri(settings.WEB3_RPC_URL))
    _web3_instance.eth.setGasPriceStrategy(rpc_gas_price_strategy)

    if not _web3_instance.isConnected():
        _web3_instance = None
        raise Web3NotAvailableException(settings.WEB3_RPC_URL)

    return _web3_instance


W3 = get_web3() 

web3.py:

代码语言:javascript
运行
复制
class Web3NotAvailableException(Exception):
    def __init__(self, rpc_url):
        self.rpc_url = rpc_url

    def __str__(self):
        return f'WEB3_RPC_URL: {self.rpc_url}'


def get_web3():
    return settings.W3
票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/101501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档