-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (61 loc) · 2.21 KB
/
main.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
from subprocess import Popen, PIPE
from threading import Thread
from queue import Queue, Empty
import atexit
import os
import sys
agent_processes = [None, None]
t = None
q = None
def cleanup_process():
global agent_processes
for proc in agent_processes:
if proc is not None:
proc.kill()
def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close()
def js_agent(observation, configuration):
"""
a wrapper around a js agent
"""
global agent_processes, t, q
agent_process = agent_processes[observation.player]
### Do not edit ###
if agent_process is None:
if "__raw_path__" in configuration:
cwd = os.path.dirname(configuration["__raw_path__"])
else:
cwd = os.path.dirname(__file__)
agent_process = Popen(["node", "main.js"], stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=cwd)
agent_processes[observation.player] = agent_process
atexit.register(cleanup_process)
# following 4 lines from https://stackoverflow.com/questions/375427/a-non-blocking-read-on-a-subprocess-pipe-in-python
q = Queue()
t = Thread(target=enqueue_output, args=(agent_process.stderr, q))
t.daemon = True # thread dies with the program
t.start()
if observation.step == 0:
# fixes bug where updates array is shared, but the first update is agent dependent actually
observation["updates"][0] = f"{observation.player}"
# print observations to agent
agent_process.stdin.write(("\n".join(observation["updates"]) + "\n").encode())
agent_process.stdin.flush()
# wait for data written to stdout
agent1res = (agent_process.stdout.readline()).decode()
_end_res = (agent_process.stdout.readline()).decode()
while True:
try: line = q.get_nowait()
except Empty:
# no standard error received, break
break
else:
# standard error output received, print it out
print(line.decode(), file=sys.stderr, end='')
outputs = agent1res.split("\n")[0].split(",")
actions = []
for cmd in outputs:
if cmd != "":
actions.append(cmd)
return actions