Python3.x 运行时错误:事件循环已关闭 [英] Python3.x RuntimeError: Event loop is closed

查看:112
本文介绍了Python3.x 运行时错误:事件循环已关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 aiohttp/asyncio 上做错了.当我尝试在循环中从另一个文件中调用 run_my_job() 时,如果我只是在一次关闭时运行它,我的以下代码工作正常:

I'm doing something wrong with aiohttp/asyncio. I've got the following code working fine if I just run it on a one off, when I try to call run_my_job() from another file in a loop like so:

main.py
========================================
 count = 0
    batch_count = math.ceil((abc.get_count()/100))
    print("there are {} batches to complete.".format(batch_count))
    while count < batch_count:
        print("starting batch {}...".format(count))
        abc.run_my_job()
        print("batch {} completed...".format(count))
        count += 1


abc.py
===============================
def run_my_job(self):
    self.queue_manager(self.do_stuff(all_the_tasks))

def queue_manager(self, method):
    print('starting event queue')
    loop = asyncio.get_event_loop()
    future = asyncio.ensure_future(method)
    loop.run_until_complete(future)
    loop.close()

async def async_post(self, resource, session, data):
    async with session.post(self.api_attr.api_endpoint + resource, headers=self.headers, data=data) as response:
        resp = await response.read()
    return resp

async def do_stuff(self, data):
    print('queueing tasks')

    tasks = []
    async with aiohttp.ClientSession() as session:
        for row in data:
            task = asyncio.ensure_future(self.async_post('my_api_endpoint', session, row))
            tasks.append(task)
        result = await asyncio.gather(*tasks)
        self.load_results(result)
# goes on to load_results() method that parses json and updates the DB.

我收到这些错误:

Traceback (most recent call last):
  File "C:/usr/PycharmProjects/api_framework/api_framework.py", line 37, in <module>
starting event queue
    abc.run_my_job()
  File "C:\usr\PycharmProjects\api_framework\api\abc\abc.py", line 77, in run_eligibility
    self.queue_manager(self.verify_eligibility(json_data))
  File "C:\usr\PycharmProjects\api_framework\api\abc\abc.py", line 187, in queue_manager
    future = asyncio.ensure_future(method)
  File "C:\Python36x64\lib\asyncio\tasks.py", line 512, in ensure_future
    task = loop.create_task(coro_or_future)
  File "C:\Python36x64\lib\asyncio\base_events.py", line 282, in create_task
    self._check_closed()
  File "C:\Python36x64\lib\asyncio\base_events.py", line 357, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
sys:1: RuntimeWarning: coroutine 'Consumer.run_my_job' was never awaited

推荐答案

看看这个函数:

def queue_manager(self, method):
    print('starting event queue')
    loop = asyncio.get_event_loop()
    future = asyncio.ensure_future(method)
    loop.run_until_complete(future)
    loop.close()

这就是您用来安排每项作业的方法.并且,在函数结束时,关闭事件循环.因此,在第一个作业运行后,您关闭事件循环.

This is what you call to schedule each job. And, at the end of the function, you close the event loop. So, after the first job runs, you close the event loop.

如果您在此之后尝试运行更多作业,显然您是在尝试在封闭的事件循环中运行它们.(并且您有运行更多作业的作业.)因此出现错误:

If you try to run more jobs after that, you're obviously trying to run them on a closed event loop. (And you've got jobs that run further jobs.) Hence the error:

RuntimeError: Event loop is closed

只需删除 loop.close() 即可解决问题.

Just remove the loop.close() and that problem goes away.

我不确定这是否足以使您的程序正常工作,因为您没有向我们提供任何接近可运行示例的内容——此外,在您的实际代码中,run_my_job 显然是一个协程,但它不在您在此处发布的代码中.我在您发布的内容中没有看到任何其他明显的错误,但我不知道这意味着什么.

I'm not sure that will be enough to make your program work, as you haven't given us anything near a runnable example—plus, in your real code, run_my_job is apparently a coroutine, but it isn't in the code you posted here. I don't see any other blatantly obvious errors in what you posted, but I don't know how much that mean.

这篇关于Python3.x 运行时错误:事件循环已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆