77from common .log_helper import LOGGER
88from bot .local .engine import get_engine
99from bot .bot import BotMjai , GameMode
10- from common .settings import Settings
11- import multiprocessing
12-
13- # 尝试获取mjai.bot实例,该方法可能会导致 panick,需要在分离进程中使用
14- def create_bot_instance (queue ):
15- import riichi3p
16- try :
17- # 尝试创建一个mjai.bot实例
18- riichi3p .online .Bot (1 )
19- queue .put (True ) # 将成功的标志放入队列
20- except Exception as e :
21- LOGGER .warning ("Cannot create bot: %s" , e , exc_info = True )
22- LOGGER .warning ("Could be missing model.pth file in path ./mjai/bot_3p" )
23- queue .put (False ) # 将失败的标志放入队列
24-
25- # 使用分离进程尝试创建bot实例
26- def try_create_ot2_bot ():
27- queue = multiprocessing .Queue ()
28- process = multiprocessing .Process (target = create_bot_instance , args = (queue ,))
29- process .start ()
30-
31- # 尝试从队列中获取结果,设置超时时间防止无限等待
32- try :
33- success = queue .get (timeout = 3 )
34- except Exception as e :
35- LOGGER .error ("Failed to retrieve the result from the subprocess: %s" , e )
36- success = False
37-
38- process .join ()
39-
40- if not success or process .exitcode != 0 :
41- LOGGER .error ("Failed to create bot or detected a crash in the subprocess with exit code %s" , process .exitcode )
42- return False
43- return True
4410
4511
4612class BotMortalLocal (BotMjai ):
@@ -64,28 +30,13 @@ def __init__(self, model_files:dict[GameMode, str]) -> None:
6430 except Exception as e :
6531 LOGGER .warning ("Cannot create engine for mode %s: %s" , k , e , exc_info = True )
6632 elif k == GameMode .MJ3P :
67- settings = Settings ()
68- if settings .enable_ot2_for_3p :
69- import riichi3p
70- try :
71- # 用分离进程尝试创建一个mjai.bot实例
72- if try_create_ot2_bot ():
73- self ._engines [k ] = "./mjai/bot_3p/model.pth"
74- else :
75- LOGGER .warning ("Cannot create bot for OT2 model %s." , k , exc_info = True )
76- LOGGER .warning ("Could be missing model.pth file in path ./mjai/bot_3p" )
77- except Exception as e :
78- LOGGER .warning ("Cannot create bot for OT2 model %s: %s" , k , e , exc_info = True )
79- LOGGER .warning ("Could be missing model.pth file in path ./mjai/bot_3p" )
80- pass
81- else :
82- # test import libraries for 3p
83- try :
84- import libriichi3p
85- from bot .local .engine3p import get_engine as get_engine_3p
86- self ._engines [k ] = get_engine_3p (self .model_files [k ])
87- except Exception as e : # pylint: disable=broad-except
88- LOGGER .warning ("Cannot create engine for mode %s: %s" , k , e , exc_info = True )
33+ # test import libraries for 3p
34+ try :
35+ import libriichi3p
36+ from bot .local .engine3p import get_engine as get_engine_3p
37+ self ._engines [k ] = get_engine_3p (self .model_files [k ])
38+ except Exception as e : # pylint: disable=broad-except
39+ LOGGER .warning ("Cannot create engine for mode %s: %s" , k , e , exc_info = True )
8940 self ._supported_modes = list (self ._engines .keys ())
9041 if not self ._supported_modes :
9142 raise LocalModelException ("No valid model files found" )
0 commit comments