Skip to content

Instantly share code, notes, and snippets.

Created July 2, 2013 02:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5906387 to your computer and use it in GitHub Desktop.
Save anonymous/5906387 to your computer and use it in GitHub Desktop.
def add_concurrent_options(parser, prefer_threads=False):
# Adds relevant options to parser
parser.add_argument("--workers", choices=("threads", "processes"), default="threads" if prefer_threads else "processes",
help="Select if concurrency is done using threads or processes [%(default)s]")
parser.add_argument("--single", action="store_true", help="Uses a single thread for concurrency. This makes debugging a lot easier")
import multiprocessing
def get_concurrent_executor(options):
# Gets a suitable executor
if options.workers=="threads" or options.single:
workers=multiprocessing.cpu_count() if not options.single else 1
return futures.ThreadPoolExecutor(max_workers=workers)
return futures.ProcessPoolExecutor()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment