问题描述
我正在尝试收听有关infura的许多事件,但是它不起作用。只有一个过滤器起作用,当我添加第二个过滤器时,会出现以下消息:
RuntimeError: cannot call recv while another coroutine is already waiting for the next message
Exception in thread Thread-15:
大约2秒钟后,出现此错误消息:
raise ValueError(response["error"])
ValueError: {'code': -32000,'message': 'filter not found'}
这是我的python代码:
import threading
import time
from web3 import Web3
import json
PROVIDER = "wss://ropsten.infura.io/ws/v3/f5000000000000"
web3 = Web3(Web3.WebsocketProvider(PROVIDER))
truffleFile = json.load(open('/Users/a/ropsten/build/contracts/a.json'))
abi = truffleFile['abi']
bytecode = truffleFile['bytecode']
contract = web3.eth.contract(abi= abi,address= '0x000000000000000')
# ------------------------------------------------------------
def handle_event2(name,event):
print('the event is from :%s'%name)
print(event)
def log_loop2(name,event_filter,poll_interval):
while True:
for event in event_filter.get_new_entries():
handle_event2(name,event)
time.sleep(poll_interval)
def PS_listen():
print(" I am in PS Listen ")
block_filter = contract.events.PS.createFilter(fromBlock = 'latest')
worker1 = threading.Thread(target=log_loop2,args=('Th-1',block_filter,2,))
worker1.start()
print(" I am in PS Listen after start")
def handle_event2(name,event):
print('the event is from :%s'%name)
print(event)
# ---------------------------------------------
def log_loop2(name,poll_interval):
while True:
for event in event_filter.get_new_entries():
handle_event2(name,event)
time.sleep(poll_interval)
def listen():
print(" I am in Listen ")
block_filter = contract.events.PS.createFilter(fromBlock = 'latest')
print(block_filter)
worker2 = threading.Thread(target=log_loop2,args=('Th-2',))
worker2.start
print(" I am in Listen after start")
print(" This is the main function1")
PS_listen()
listen()
print(" This is the main function2")
注意:我已经正确添加了Infura项目ID。将合同地址部署在Ropsten infura上后,即可从终端处处理合同地址。
是否存在线程问题,导致我无法为下一个事件创建另一个过滤器,而错误表明协程正在另一任务中运行?
请提供任何帮助
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)