Last active
December 4, 2018 21:31
-
-
Save wesleyit/74623f819a97399d03f796749ef707b3 to your computer and use it in GitHub Desktop.
Run threads with sub processes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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