MaaFramework
MaaFramework copied to clipboard
python: 多线程并发连接 Adb 时出现错误
问题概述
Python 3.12.7 使用 Threading 库创建多线程时(线程数>=2),在连接控制器时会概率出现异常。然而,在不改变模拟器/ adb.exe 状态的前提下,重新运行脚本概率可以正常运行。(部分或全部线程正常) 目前的解决方案是在线程启动间增加延迟。
控制台报错
Exception in thread Thread-3 (run):
Traceback (most recent call last):
File "C:\Users\buliu\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1075, in _bootstrap_inner
self.run()
File "C:\Users\buliu\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1012, in run
[2025-05-10 09:39:24.615][ERR][Px10428][Tx39024][UnitBase.cpp][L64][MaaNS::CtrlUnitNs::UnitBase::startup_and_read_pipe] child return error [argv.exec=D:/MuMu Player 12-1/shell/adb.exe] [argv.args=["-s","127.0.0.1:16416","shell","settings get secure android_id"]]
[2025-05-10 09:39:24.615][ERR][Px10428][Tx39024][ControlUnitMgr.cpp][L98][MaaNS::CtrlUnitNs::ControlUnitMgr::request_uuid] failed to request_uuid
[2025-05-10 09:39:24.615][ERR][Px10428][Tx39024][GeneralControllerAgent.cpp][L46][MaaNS::ControllerNS::GeneralControllerAgent::_request_uuid] controller request_uuid failed
[2025-05-10 09:39:24.616][ERR][Px10428][Tx39024][ControllerAgent.cpp][L841][MaaNS::ControllerNS::ControllerAgent::request_uuid] controller request uuid failed
self._target(*self._args, **self._kwargs)
File "c:\Users\buliu\Desktop\Github\Auto-Roll\src\maafw\core.py", line 62, in run
tasker = cls.create_tasker(res, ctrl)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\buliu\Desktop\Github\Auto-Roll\src\maafw\core.py", line 134, in create_tasker
[2025-05-10 09:39:24.737][ERR][Px10428][Tx3113][UnitBase.cpp][L64][MaaNS::CtrlUnitNs::UnitBase::startup_and_read_pipe] child return error [argv.exec=D:/MuMu Player 12-1/shell/adb.exe] [argv.args=["-s","127.0.0.1:16448","shell","settings get secure android_id"]]
[2025-05-10 09:39:24.737][ERR][Px10428][Tx37485][UnitBase.cpp][L64][MaaNS::CtrlUnitNs::UnitBase::startup_and_read_pipe] child return error [argv.exec=D:/MuMu Player 12-1/shell/adb.exe] [argv.args=["-s","127.0.0.1:16480","shell","settings get secure android_id"]]
[2025-05-10 09:39:24.738][ERR][Px10428][Tx37485][ControlUnitMgr.cpp][L98][MaaNS::CtrlUnitNs::ControlUnitMgr::request_uuid] failed to request_uuid
[2025-05-10 09:39:24.738][ERR][Px10428][Tx3113][ControlUnitMgr.cpp][L98][MaaNS::CtrlUnitNs::ControlUnitMgr::request_uuid] failed to request_uuid
[2025-05-10 09:39:24.738][ERR][Px10428][Tx37485][GeneralControllerAgent.cpp][L46][MaaNS::ControllerNS::GeneralControllerAgent::_request_uuid] controller request_uuid failed
[2025-05-10 09:39:24.739][ERR][Px10428][Tx3113][GeneralControllerAgent.cpp][L46][MaaNS::ControllerNS::GeneralControllerAgent::_request_uuid] controller request_uuid failed
[2025-05-10 09:39:24.739][ERR][Px10428][Tx37485][ControllerAgent.cpp][L841][MaaNS::ControllerNS::ControllerAgent::request_uuid] controller request uuid failed
[2025-05-10 09:39:24.740][ERR][Px10428][Tx3113][ControllerAgent.cpp][L841][MaaNS::ControllerNS::ControllerAgent::request_uuid] controller request uuid failed
ctrl.post_connection().wait()
File "C:\Users\buliu\Desktop\Github\Auto-Roll\.venv\Lib\site-packages\maa\job.py", line 17, in wait
self._wait_func(self._job_id)
File "C:\Users\buliu\Desktop\Github\Auto-Roll\.venv\Lib\site-packages\maa\controller.py", line 168, in _wait
return Library.framework().MaaControllerWait(self._handle, maaid)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ctypes.ArgumentError: argument 1: OverflowError: int too long to convert
最小复现代码
import time
import threading
from pathlib import Path
from maa.controller import AdbController
from maa.define import MaaAdbScreencapMethodEnum
from maa.resource import Resource
from maa.tasker import Tasker
from maa.toolkit import Toolkit
DEVICES = {
"127.0.0.1:16384": {
"extras": {
"mumu": {
"enable": True,
"index": 0,
"path": "YOUR_PATH",
}
}
},
"127.0.0.1:16416": {
"extras": {
"mumu": {
"enable": True,
"index": 1,
"path": "YOUR_PATH",
}
}
},
}
ADB_PATH = Path("YOUR_ADB_PATH")
RES_PATH = Path("YOUR_RESOURCE_PATH")
ENTRY_NODE = "YOUR_ENTRY_NODE_NAME"
Toolkit.init_option("./")
def run(address: str, config: dict):
res = Resource()
res.post_bundle(RES_PATH).wait()
ctrl = AdbController(
ADB_PATH,
address,
screencap_methods=MaaAdbScreencapMethodEnum.EmulatorExtras,
config=config,
) # 使用了截图增强
ctrl.post_connection().wait()
tasker = Tasker()
tasker.bind(res, ctrl)
tasker.post_task(ENTRY_NODE).wait()
if __name__ == "__main__":
th_list: list[threading.Thread] = []
for address, config in DEVICES.items():
th = threading.Thread(target=run, args=[address, config], daemon=True)
th_list.append(th)
for th in th_list:
th.start()
time.sleep(2) # If don't sleep, will occur error.
日志文件
maa.log 我个人没能在日志中发现什么异常。另外我建议最好以运行最小复现产生的 log 为准,这个有一次成功了()