-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
350 lines (289 loc) · 11.2 KB
/
bot.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import threading
import time
from subprocess import PIPE, Popen
from log import logger
import config
import copy
MEGABYTE = 1 << 20
class ExecuteError(OSError):
'''
This exception is raised when create_process
cannot start bot process (e.g. invalid command)
'''
def __init__(self):
super().__init__('can\'t start bot process')
class ProcessNotRunningException(OSError):
'''
This exception is raised after trying to
send command to process that is not running.
'''
def __init__(self):
super().__init__('process isn\'t running')
class TimeLimitException(OSError):
'''
This exception is raised when bot's process exceeded time limit.
'''
def __init__(self):
super().__init__('time limit exceeded')
class BaseBot:
'''
This class wraps bot's process and stores its information.
Examples:
>>> from move import Move
>>> from player_state import PlayerState # import *your* classes
>>> p = BaseBot('python bot.py')
>>> p.create_process()fixes
>>> state = PlayerState(...)
>>> move = Move(...)
>>> p.get_move(state, serialize, deserialize)
<move.Move object at ...>
>>> p.kill_process()
'''
def __init__(self, player_command):
'''
Constructor for class Bot.
`player_command` is a string which is used to invoke bot program.
'''
self._player_command = player_command
self._process = None
self._count_of_moves = 0
def create_process(self):
'''
Starts bot's process.
'''
logger.info('executing \'%s\'', self._player_command)
try:
self._process = psutil.Popen(
self._player_command.split(),
stdout=PIPE,
stdin=PIPE,
stderr=PIPE
)
except OSError:
logger.critical('executing of \'%s\' failed: invalid command',
self._player_command)
raise ExecuteError
logger.info('executing successful')
def _get_real_time(self):
'''
Returns real time used by bot's process.
'''
return time.time()
def get_move(self, player_state, serialize, deserialize):
'''
Serialize player_state and transfer it to bot,
then deserialize output received from bot to `move`.
`player_state` - object, which will be passed to
the bot after serialization.
`serialize(player_state, writable_stream)` is a function,
which is responsible for serializing `player_state` and
writing it to `writable_stream`.
`deserialize(readable_stream)` is a function, which is
responsible for reading data from `readable_stream` and turning it
to a valid `move` object.
If bot's process isn't running, raise ProcessNotRunningException..
'''
if self._process is None:
raise ProcessNotRunningException()
real_time = self._get_real_time()
self._get_move_exception = None
get_move_thread = threading.Thread(
target=self._get_move,
args=(player_state, serialize, deserialize)
)
get_move_thread.start()
if self._count_of_moves % config.time_limit_count_of_moves == 0:
self._real_time_remainder = 0
self._count_of_moves += 1
real_time_start = self._get_real_time()
try:
while get_move_thread.is_alive():
real_time = self._get_real_time()
if real_time - real_time_start + self._real_time_remainder\
> config.real_time_limit_seconds:
self.kill_process()
logger.error('bot with cmd \'%s\' exceeded time limit',
self._player_command)
raise TimeLimitException
if hasattr(config, 'observe_period'):
time.sleep(config.observe_period)
finally:
self._real_time_remainder += real_time - real_time_start
if self._get_move_exception:
logger.error('exception has been raised during '
'interaction with bot')
raise self._get_move_exception
logger.debug('elapsed real time: %f sec',
self._get_real_time() - real_time)
return self._deserialize_result
def _get_move(self, player_state, serialize, deserialize):
'''
Serialize player_state with bot's `stdin`.
`stdin` is a stream opened to write per *byte*.
Deserialize move with bot's `stdout`.
`stdout` is a stream opened to read per *byte*.
Invokes author's deserialization function and
stores received `move` into self._deserialize_result.
If anyone raised an exception, this method stores
information about exception for re-raising.
'''
try:
serialize(player_state, self._process.stdin)
self._deserialize_result = deserialize(self._process.stdout)
except (BaseException, Exception) as exc:
self._get_move_exception = exc
def kill_process(self):
'''
Kills bot's process if it is running or does nothing
if the process was already killed.
'''
if self._process is not None:
try:
self._process.kill()
self._process.communicate()
except OSError:
pass
logger.info('process with cmd line \'%s\' was killed',
self._player_command)
def __del__(self):
'''
Destructor for class bot.
It automatically kills bot's process on delete.
'''
self.kill_process()
class ComplexBot(BaseBot):
'''
This class wraps bot's process and stores its information.
Examples:
>>> from move import Move
>>> from player_state import PlayerState # import *your* classes
>>> p = BaseBot('python bot.py')
>>> p.create_process()
>>> state = PlayerState(...)
>>> move = Move(...)
>>> p.get_move(state, serialize, deserialize)
<move.Move object at ...>
>>> p.kill_process()
'''
def create_process(self):
'''
Starts bot's process.
'''
import psutil
logger.info('executing \'%s\'', self._player_command)
try:
self._process = psutil.Popen(
self._player_command.split(),
stdout=PIPE,
stdin=PIPE,
stderr=PIPE
)
except OSError as e:
logger.critical('executing of \'%s\' failed: invalid command',
self._player_command)
raise ExecuteError
logger.info('executing successful')
def _get_cpu_time(self):
'''
Returns CPU time used by bot's process.
'''
times = self._process.get_cpu_times()
return times.system + times.user
def _get_memory(self):
'''
Returns memory used by process in *megabytes*.
'''
return self._process.get_memory_info().rss / MEGABYTE
def get_move(self, player_state, serialize, deserialize):
'''
Serialize player_state and transfer it to bot,
then deserialize output received from bot to `move`.
`player_state` - object, which will be passed to
the bot after serialization.
`serialize(player_state, writable_stream)` is a function,
which is responsible for serializing `player_state` and
writing it to `writable_stream`.
`deserialize(readable_stream)` is a function, which is
responsible for reading data from `readable_stream` and turning it
to a valid `move` object.
If bot's process isn't running, raise ProcessNotRunningException.
'''
import psutil
if self._process is None:
raise ProcessNotRunningException()
try:
real_time = self._get_real_time()
cpu_time = self._get_cpu_time()
self._get_move_exception = None
get_move_thread = threading.Thread(
target=self._get_move,
args=(player_state, serialize, deserialize)
)
get_move_thread.start()
if self._count_of_moves % config.time_limit_count_of_moves == 0:
self._real_time_remainder = 0
self._cpu_time_remainder = 0
self._count_of_moves += 1
real_time_start = self._get_real_time()
cpu_time_start = self._get_cpu_time()
try:
while get_move_thread.is_alive():
real_time = self._get_real_time()
cpu_time = self._get_cpu_time()
real_executing_time = real_time - real_time_start +\
self._real_time_remainder
if real_executing_time > config.real_time_limit_seconds:
self.kill_process()
logger.error('bot with cmd \'%s\' exceeded '
'time limit', self._player_command)
raise TimeLimitException
cpu_executing_time = (cpu_time - cpu_time_start +
self._cpu_time_remainder)
if cpu_executing_time > config.cpu_time_limit_seconds:
self.kill_process()
logger.error('bot with cmd \'%s\' exceeded '
'cpu time limit', self._player_command)
raise TimeLimitException
if hasattr(config, 'observe_period'):
time.sleep(config.observe_period)
finally:
self._real_time_remainder += real_time - real_time_start
self._cpu_time_remainder += cpu_time - cpu_time_start
if self._get_move_exception:
logger.error('exception has been raised during '
'interaction with bot')
raise self._get_move_exception
'''logger.debug('elapsed real time: %f sec, '
'elapsed cpu time: %f sec, '
'used memory: %f mb',
self._get_real_time() - real_time,
self._get_cpu_time() - cpu_time,
self._get_memory())'''
return self._deserialize_result
except psutil.NoSuchProcess:
raise ProcessNotRunningException()
def kill_process(self):
'''
Kills bot's process if it is running or does nothing
if the process was already killed.
'''
import psutil
if self._process is not None:
try:
self._process.kill()
self._process.communicate()
except (OSError, psutil.NoSuchProcess):
pass
logger.info('process with cmd line \'%s\' was killed',
self._player_command)
def is_psutil():
'''
Returns if psutil is installed.
'''
try:
import psutil
except ImportError:
return False
else:
return True
Bot = ComplexBot if is_psutil() else BaseBot