Skip to content

Commit

Permalink
[examples] Fix scoped thread pool.
Browse files Browse the repository at this point in the history
This ensures that the thread pool is destroyed after use.
  • Loading branch information
ChrisCummins committed Oct 11, 2021
1 parent 2f00774 commit 7c10cff
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions examples/explore.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,11 @@ def number_list(stats):
# Compute an action graph and use it to find the optimal sequence
# within episode_length actions. Uses as many threads as there are
# elements in envs.
def compute_action_graph(envs, episode_length):
def compute_action_graph(pool, envs, episode_length):
assert len(envs) >= 1
env_queue = Queue()
for env in envs:
env_queue.put(env)
pool = ThreadPool(len(envs))

stats = NodeTypeStats(action_count=env.action_space.n)
graph = StateGraph(edges_per_node=env.action_space.n)

Expand Down Expand Up @@ -448,7 +446,8 @@ def main(argv):
try:
for _ in range(FLAGS.nproc):
envs.append(make_env())
compute_action_graph(envs, episode_length=FLAGS.episode_length)
with ThreadPool(len(envs)) as pool:
compute_action_graph(pool, envs, episode_length=FLAGS.episode_length)
finally:
for env in envs:
env.close()
Expand Down

0 comments on commit 7c10cff

Please sign in to comment.