我用:
python3.7
,
web3==4.9.2
,
Infura
和/或own Node
。
初始化web3
后,它只在第一次返回isConnected()
响应,然后冻结。
web3.py:
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
日志:
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,一切都会正常工作。
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
怎么搞错了?
发布于 2021-06-07 20:29:01
我将web3
初始化移到了设置文件中,现在它只初始化了一次,我已经获得了Celery
的web3-结果:
设置/生产.production.py:
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:
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
https://ethereum.stackexchange.com/questions/101501
复制相似问题