Skip to content

Instantly share code, notes, and snippets.

@gaqzi
Last active September 24, 2016 04:15
Show Gist options
  • Save gaqzi/57a95406461e3ed3c48e390f9219881c to your computer and use it in GitHub Desktop.
Save gaqzi/57a95406461e3ed3c48e390f9219881c to your computer and use it in GitHub Desktop.
class CLI(object):
# […snip…]
def run(self, command_name, paths):
if isinstance(command_name, Command):
command = command_name
else:
command = self.find_command(command_name)
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool_executor:
futures = self._schedule_command(command, paths, pool_executor)
self._resolve_futures(futures)
return self.results
def _schedule_command(self, command, paths, pool_executor):
futures = dict()
for path, cmd in command.items(filter=paths):
if cmd is None:
continue
self.outputter.info.write('Running {0} for {1}:\n'.format(command.name, path))
futures[pool_executor.submit(self.executor.execute, path, cmd)] = path
self.outputter.info.write('\n')
return futures
def _resolve_futures(self, futures):
for future in concurrent.futures.as_completed(futures):
path = futures[future]
try:
self.results.add(future.result())
except Exception as exc:
self.outputter.error.write(
u'{0!r:s} generated an exception: {1:s}\n'.format(path, exc)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment