Python 3.9 关闭默认执行器

问题描述

如果在使用 RuntimeError调用认执行程序,Python 3.9 会引发 loop.shutdown_default_executor

RuntimeError: Executor shutdown has been called

我这样开始我的程序:

b = Lambda()

try:
    b.loop.run_forever()
except:
    b.loop.run_until_complete(b.stop())
finally:
    b.release()

大多数情况下,except 子句在我结束所有任务的地方正常执行,包括关闭执行程序:

try:
    await asyncio.wait({asyncio.ensure_future(task)
        for task in [
            self.loop.shutdown_default_executor(),self.loop.shutdown_asyncgens(),self.session.close(),self.close(),]
    })
finally:
    tasks = [
        t for t in asyncio.all_tasks() 
        if t is not asyncio.current_task()
    ]
    [t.cancel() for t in tasks]

    await asyncio.gather(*tasks,return_exceptions=True)

但有一小部分时间,b.loop.run_forever 会捕获 KeyboardInterrupt,然后被 RuntimeError 捕获,因此不会执行常规的 except 块。

回溯仅指向 b.loop.run_forever 作为原始原因,但这是我使用执行程序的方式:

def _cancel(tasks): 
    async def wrapper():
        {t.cancel() for t in tasks}
        return await asyncio.gather(*tasks,return_exceptions=True)
    return asyncio.run_coroutine_threadsafe(wrapper(),self.loop).result()

self.loop.call_soon_threadsafe(self.loop.run_in_executor,None,lambda: _cancel(pending))

所以我认为我还需要停止该线程的循环,但我不确定。

如果它有一些意义,我正在手动创建和设置认执行器:

executor  = concurrent.futures.ThreadPoolExecutor(max_workers=3)
self.loop.set_default_executor(executor)

很难重现错误,因为它不会每次都发生。我猜它会在我 KeyboardInterrupt 执行程序再次开始运行时发生。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...