I’m having a hard time trying to create child processes for every folder in the current directory to execute certain function on them (Python 2.7).
My idea is to:
- First get all the folders with os.listdir(path) and filter the files with os.path.isdir(file_path).
- Count the folders and calculate how many forks are needed, e.g: for 5 folders I’d need 3 forks (2³ – 1 = 7 child processes)
-
Iterate through the number of needed forks and in each iteration execute the code for the folders, two at a time, getting their paths from a list containing all the folder paths.
for counter in needed_forks: folder_pid = os.fork() if pid: some_function(folder_list[counter]) else: some_function(folder_list[counter+1])
I can’t get any good output mostly due to, I think, one index of the folder_list repeating in each iteration.
Any help is really appreciated, I’m rushing on this assignment, trying to learn multiprocessing and Python at once.
Thanks.
Why not just have one process keep forking? Then you don’t have to try to calculate the number of forks to use:
Of course, I’d question the wisdom of using
forkto handle directories this way…