我有一个代码在一个循环中运行,用Python3.6编写。在循环内部,我通过web3.py获取数据。我想异步处理该数据。我用过asyncio。这是我的代码:
web3 = Web3(HTTPProvider(cfg.eth_node_url))
web3_pending_filter = web3.eth.filter('pending')
event_loop = asyncio.get_event_loop()
taskLock = Lock()
tasks = []
def main():
thread = threading.Thread(target=start_event_loop)
thread.start()
while True:
process_pending_trx(web3, web3_pending_filter)
time.sleep(0.001)
def start_event_loop():
while True:
buf = []
with taskLock:
for task in tasks:
buf.append(task)
tasks = []
if len(buf) > 0:
event_loop.run_until_complete(buf)
time.sleep(0.0001)
def process_pending_trx(web3, web3_filter):
print('Scanning for new pending transactions...')
transaction_list = web3.eth.getFilterChanges(web3_filter.filter_id)
print(len(transaction_list))
for trx_hash in transaction_list:
with taskLock:
tasks.append(process_pending_transaction(web3, trx_hash))
async def process_pending_transaction(web3, trx_hash):
print(trx_hash)
trx = web3.eth.getTransaction(trx_hash)
...
await asyncio.sleep(0)
start_event_loop 行
的方法event_loop.run_until_complete(BUF) 我收到错误:“asyncio.Future,coroutine或awaitable is required”
出现此错误的原因可能是什么?或者我如何异步运行 process_pending_transaction 方法?也许还有其他一些方法。