python - 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
它时会抛出错误如何读取和循环合同?
我做得对吗?请看一下。
我该如何解决。
解决方案
推荐阅读
- c - fseek 是做什么的?
- sql - 为什么在 Postgres 中的 RAM 和磁盘中排序时我的查询速度较慢?
- python - 我该如何解决这个python问题?(Flask)
- java - Spring Batch:无法使用不同的JobParameters启动Job并且无法访问JobParameters
- apache - Apache mod_rewrite 路径以查询特定路径格式的字符串
- javascript - 为什么我的 :search-input.sync 函数被调用了两次?
- java - Spring 共享服务和存储库 - 最佳实践
- javascript - 从 javascript/typescript 中的 XML DOM 解析媒体 url 的最佳方法是什么?
- python - 在 IAR 上运行转换为 C 的 python 代码
- less - Less 不会应用带有点 (.) 的 Tailwind CSS 类名