解决RuntimeError: cannot schedule new futures after interpreter shutdown

文章讨论了在Python中使用多线程时遇到的RuntimeError:cannotschedulenewfuturesafterinterpretershutdown问题,解决方法是将原本在子线程中执行的receive_response函数改为主线程,并在主线程中使用submit。
Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

最近在使用python多线程的时候遇到了这个问题,将函数提交到线程中总是会报错RuntimeError: cannot schedule new futures after interpreter shutdown,这个问题主要是由于在子线程中调用了submit方法,例如如果在主线程中调用就不会出现上述问题。

例如,我的函数receive_response(self)函数是作为一个子线程开启的,所以一开始会报错

class Client:
    def init(self, args):
        # init 部分代码省略
        # 创建线程池来处理回复的消息
        self.command_executor = concurrent.futures.ThreadPoolExecutor(max_workers=3)  # 假设我们设置最大线程数为3
        # 创建线程来接收回复的消息
        self.receive_response_thread = threading.Thread(target=self.receive_response)
        self.receive_response_thread.start()

    def receive_response(self):
        # 部分代码省略
        while True:
            # ... 接收command操作
            self.command_executor.submit(self.handle_command, command)
            # ... 其他操作

    def handle_command(self, command)
        pass
      

修正后的代码如下,我将receive_response设置为主线程(之前所有函数都是子线程,没有设置主线程),在主线程中使用submit函数,就能正常使用了,记得主线程需要用死循环包裹,不能停止。

class Client:
    def __init__(self, args):
        # 部分无关代码省略
        # 创建线程池来处理回复的消息
        self.command_executor = concurrent.futures.ThreadPoolExecutor(max_workers=3)  # 线程池大小

        # 创建线程来处理接收消息的任务(主线程)
        self.receive_response()  # 就是这边改了一下

    def receive_response(self):
        # 部分代码省略
        while True:
            # ... 接收command操作
            self.command_executor.submit(self.handle_command, command)
            # ... 其他操作

    def handle_command(self, command)
        pass
      

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

ros2报错[WARN] [1762228025.693300833] [camera_calibration]: plan error! Traceback (most recent call last): File "/usr/lib/python3.12/runpy.py", line 198, in _run_module_as_main return _run_code(code, main_globals, None, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/runpy.py", line 88, in _run_code exec(code, run_globals) File "/home/qq/.vscode/extensions/ms-python.debugpy-2025.14.1-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 71, in <module> 71, in <module> cli.main() File "/home/qq/.vscode/extensions/ms-python.debugpy-2025.14.1-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 508, in maincli.py", line 508, in main run() File "/home/qq/.vscode/extensions/ms-python.debugpy-2025.14.1-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 358, in run_filecli.py", line 358, in run_file runpy.run_path(target, run_name="__main__") File "/home/qq/.vscode/extensions/ms-python.debugpy-2025.14.1-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 310, in run_pathrun_path return _run_module_code(code, init_globals, run_name, pkg_name=pkg_name, script_name=fname)ript_name=fname) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/qq/.vscode/extensions/ms-python.debugpy-2025.14.1-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 127, in _run_module_code_run_module_code _run_code(code, mod_globals, init_globals, mod_name, mod_spec, pkg_name, script_name)ript_name) File "/home/qq/.vscode/extensions/ms-python.debugpy-2025.14.1-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 118, in _run_code_run_code exec(code, run_globals) File "/home/qq/hhq/control_move/src/lkai/script_ros2/path_camera_calibration.py", line 438, in <module> path_calibrate_dynamic_standard(1) File "/home/qq/hhq/control_move/src/lkai/script_ros2/path_camera_calibration.py", line 104, in path_calibrate_dynamic_standard pose_endlink, pose_obj = audo_face_to_board(sim, center=(0, 0, -dis), type = "1")= "1") ^^^^^^^^^^^^^^^^^^^^^^ TypeError: cannot unpack non-iterable NoneType object Exception in thread Thread-8 (spin): Traceback (most recent call last): File "/usr/lib/python3.12/threading.py", line 1073, in _bootstrap_inner self.run() File "/usr/lib/python3.12/threading.py", line 1010, in run self._target(*self._args, **self._kwargs) File "/opt/ros/jazzy/lib/python3.12/site-packages/rclpy/executors.py", line 308, in spin08, in spin self.spin_once() File "/opt/ros/jazzy/lib/python3.12/site-packages/rclpy/executors.py", line 911, in spin_once11, in spin_once self._spin_once_impl(timeout_sec) File "/opt/ros/jazzy/lib/python3.12/site-packages/rclpy/executors.py", line 901, in _spin_once_impl self._executor.submit(handler) File "/usr/lib/python3.12/concurrent/futures/thread.py", line 172, in submit raise RuntimeError('cannot schedule new futures after ' RuntimeError: cannot schedule new futures after interpreter shutdown sys:1: RuntimeWarning: coroutine 'Executor._make_handler.<locals>.handler' was never awaitednever awaited RuntimeWarning: Enable tracemalloc to get the object allocation traceback
最新发布
11-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值