asyncio oracle 异步,带有asyncio futures和RuntimeError的InvalidStateError与aiohttp时使用期货回调...

编程知识 行业动态 更新时间:2024-06-13 00:17:18

我是asyncio和aiohttp新手。我目前得到这个错误,不知道为什么我收到InvalidStateError我asyncio未来RuntimeError为我的会议:带有asyncio futures和RuntimeError的InvalidStateError与aiohttp时使用期货回调

Traceback (most recent call last):

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/runpy.py", line 184, in _run_module_as_main

"__main__", mod_spec)

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/runpy.py", line 85, in _run_code

exec(code, run_globals)

File "/Users/bli1/Development/QE/idea/trinity-tracer/tracer/tests/tracer.py", line 100, in

sys.exit(main(sys.argv))

File "/Users/bli1/Development/QE/idea/trinity-tracer/tracer/tests/tracer.py", line 92, in main

poster.post()

File "/Users/bli1/Development/QE/idea/trinity-tracer/tracer/utils/poster.py", line 87, in post

results = event_loop.run_until_complete(self.async_post_events(events))

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/base_events.py", line 387, in run_until_complete

return future.result()

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/futures.py", line 274, in result

raise self._exception

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/tasks.py", line 239, in _step

result = coro.send(None)

File "/Users/bli1/Development/QE/idea/trinity-tracer/tracer/utils/poster.py", line 79, in async_post_events

task.add_done_callback(self.send_oracle, task.result(), session)

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/futures.py", line 268, in result

raise InvalidStateError('Result is not ready.')

asyncio.futures.InvalidStateError: Result is not ready.

Task exception was never retrieved

future: exception=RuntimeError('Session is closed',)>

Traceback (most recent call last):

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/tasks.py", line 239, in _step

result = coro.send(None)

File "/Users/bli1/Development/QE/idea/trinity-tracer/tracer/utils/poster.py", line 64, in async_post_event

async with session.post(self.endpoint, data=event) as resp:

File "/Users/bli1/Development/QE/idea/trinity-tracer/lib/python3.5/site-packages/aiohttp/client.py", line 565, in __aenter__

self._resp = yield from self._coro

File "/Users/bli1/Development/QE/idea/trinity-tracer/lib/python3.5/site-packages/aiohttp/client.py", line 161, in _request

raise RuntimeError('Session is closed')

RuntimeError: Session is closed

我所试图做的是POST到端点,然后使用相同的事件POST编辑发布到另一个端点。这将是跑了作为另一async方法作为callback

这里是我的代码:

async def async_post_event(self, event, session):

async with session.post(self.endpoint, data=event) as resp:

event["tracer"]["post"]["timestamp"] = time.time() * 1000.0

event["tracer"]["post"]["statusCode"] = await resp.status

return event

async def send_oracle(self, event, session):

async with session.post(self.oracle, data=event) as resp:

return event["event"]["event_header"]["event_id"], await resp.status

async def async_post_events(self, events):

tasks = []

conn = aiohttp.TCPConnector(verify_ssl=self.secure)

async with aiohttp.ClientSession(connector=conn) as session:

for event in events:

task = asyncio.ensure_future(self.async_post_event(event, session))

task.add_done_callback(self.send_oracle, task.result(), session)

tasks.append(task)

return await asyncio.gather(*tasks)

def post(self):

event_loop = asyncio.get_event_loop()

try:

events = [self.gen_random_event() for i in range(self.num_post)]

results = event_loop.run_until_complete(self.async_post_events(events))

print(results)

finally:

event_loop.close()

更多推荐

asyncio oracle 异步,带有asyncio futures和RuntimeError的InvalidStateError与aiohttp时使用期货回

本文发布于:2023-03-25 01:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/9f30a1234b6a1ef1becb2d13b915a237.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:回调   期货   futures   oracle   asyncio

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!