首页 > 解决方案 > django使用sync_to_async方式获取协程对象数据

问题描述

def compute_topics_to_eventabis() -> Dict:
    "Loop through all contracts in our DB and compute topics to ABI mapping"
    topics_to_event_abis = {}
    contracts = sync_to_async(EthereumContract.objects.all)()
    print(contracts)
    for contract in contracts:
        # we could use contract.web3_objects().events as well
        for entity in contract.abi:
            if entity["type"] == u"event":
                topics = construct_event_topic_set(entity, get_infura_server().codec)
                for topic in topics:
                    topics_to_event_abis[topic] = entity
    return topics_to_event_abis




Task exception was never retrieved
future: <Task finished name='Task-2' coro=<get_new_heads() done, defined at watcher.py:145> exception=TypeError("'coroutine' object is not iterable")>
Traceback (most recent call last):
File "watcher.py", line 173, in get_new_heads
    process_block(formatted_result['blockNumber'],
File "watcher.py", line 142, in process_block
    return replay_block(block_id, get_infura_server())
File "watcher.py", line 132, in replay_block
    return process_each_log_and_spawn_event(logs)
File "watcher.py", line 117, in process_each_log_and_spawn_event
    for event in process_each_log_and_yield(logs):
File "watcher.py", line 105, in process_each_log_and_yield
    TOPICS_TO_EVENT_ABIS = compute_topics_to_eventabis()
File "watcher.py", line 93, in compute_topics_to_eventabis
    for contract in contracts:
TypeError: 'coroutine' object is not iterable
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/events.py:81: RuntimeWarning: coroutine 'SyncToAsync.__call__' was never awaited
self._context.run(self._callback, *self._args)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

在这里,当我使用sync_to_async它时会抛出错误如何读取和循环合同?

我做得对吗?请看一下。

我该如何解决。

标签: pythondjangoasynchronouscoroutine

解决方案


推荐阅读