Skip to content

Instantly share code, notes, and snippets.

@wesleyit
Last active December 4, 2018 21:31
Show Gist options
  • Select an option

  • Save wesleyit/74623f819a97399d03f796749ef707b3 to your computer and use it in GitHub Desktop.

Select an option

Save wesleyit/74623f819a97399d03f796749ef707b3 to your computer and use it in GitHub Desktop.
Run threads with sub processes
from multiprocessing import Pool as pool
from subprocess import Popen as run
from os import listdir as ls
# all files begining with this will be executed
PREFIX = 'python_scripts_'
# set for the number of cores you have
THREADS = 4
def execute(cmd):
print(f'\nExecuting {cmd}...')
pid = run(['python3', cmd])
pid.wait()
print(f'Finished {cmd}!\n')
def get_files():
lens = lambda x: x.startswith(PREFIX)
files = ls()
files = filter(lens, files)
files = list(files)
return files
if __name__ == '__main__':
print(f'Starting the pool with {THREADS} threads...')
p = pool(THREADS)
files = get_files()
p.map(execute, files)
print('Done! Ending all threads.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment