我是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时使用期货回