-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathFramework.py
More file actions
81 lines (58 loc) · 1.96 KB
/
Framework.py
File metadata and controls
81 lines (58 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Under MIT License, see LICENSE.txt
import logging
import os
import signal
from multiprocessing import Queue, Manager
import sys
import time
from Engine.engine import Engine
from ai.coach import Coach
from Util.timing import create_fps_timer
from config.config import Config
config = Config()
class Framework:
QUEUE_SIZE = 100
def __init__(self, profiling=False):
self.logger = logging.getLogger(self.__class__.__name__)
self.profiling = profiling
self.game_state = Manager().dict()
self.field = Manager().dict()
maxsize = config['FRAMEWORK']['max_queue_size']
self.ai_queue = Queue(maxsize=maxsize)
self.referee_queue = Queue(maxsize=maxsize)
self.ui_send_queue = Queue(maxsize=maxsize)
self.ui_recv_queue = Queue(maxsize=maxsize)
self.engine = Engine(self)
self.coach = Coach(self)
self.watchdogs = {
self.engine.name: Manager().Value('f', time.time()),
self.coach.name: Manager().Value('f', time.time())
}
def start(self):
self.engine.start()
self.coach.start()
sleep = create_fps_timer(config['FRAMEWORK']['subprocess_check_time'])
try:
while self.coach.is_alive() and self.engine.is_alive():
sleep()
except SystemExit:
pass
except KeyboardInterrupt:
self.logger.debug('Interrupted.')
except BrokenPipeError:
self.logger.exception('A connection was broken.')
except:
self.logger.exception('An error occurred.')
finally:
self.stop_game()
def stop_game(self):
self.logger.critical('Framework stopped.')
try:
os.kill(self.engine.pid, signal.SIGINT)
except ProcessLookupError:
pass
try:
os.kill(self.coach.pid, signal.SIGINT)
except ProcessLookupError:
pass
sys.exit()