Ether
isConnected() 凍結並且在 web3.py 中不返回任何內容
我使用:
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()
之後 docker 容器凍結並且什麼也沒有發生。雖然
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
怎麼了?
我將
web3
初始化移至設置文件,現在它已初始化一次,並且我已經獲得了 web3-resultCelery
:設置/生產.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