Python Forum
Multi-processing - problem with running multiple *.py files at the same time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multi-processing - problem with running multiple *.py files at the same time
#1
Hi there,

I have multiple *.py files that I need to run concurrently. Each file looks like this:

def my_function(file):

    # my code
Then I have written this to run my files simultaneously:

import multiprocessing

from file1 import my_function

if __name__ == '__main__':
    __spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)" # This line is because I am using Spyder as my IDE

    files = ["path/to/file1.py","path/to/file2.py","path/to/file3.py","path/to/file4.py","path/to/file5.py"]

    for item in files:
        p = multiprocessing.Process(target=my_function, args=(item,))
        p.start()
        p.join()
        print("Done!")
The issue is that, however, the files are still being run one after another like in a sequence but not in parallel! Do you have any ideas how I may run my files concurrently?
Reply
#2
How do you know they are not run in parallel? Note that your program will print"Done" after every thread is started. To print when they are done, use something like
import multiprocessing
import time

    files = ["path/to/file1.py","path/to/file2.py",
             "path/to/file3.py","path/to/file4.py",
             "path/to/file5.py"]
 
    process_list=[]
    for item in files:
        p = multiprocessing.Process(target=my_function, args=(item,))
        p.start()
        process_list.append(p)

    still_alive=True
    while still_alive:
        print("-"*50)
        still_alive=False
        for p in process_list:
            if p.is_alive():
                still_alive=True
                print(p)
                time.sleep(0.2)
                p.join()

    print("finished") 
Reply
#3
I check the task manager and it's only one core of the CPU that is working; plus the time is 5 times more than running a single file (I have 5 files in the loop as you see). The point is, how do I run the for loop in parallel?

(Sep-11-2018, 04:16 AM)Antonio Wrote: Hi there,

I have multiple *.py files that I need to run concurrently. Each file looks like this:

def my_function(file):

    # my code
Then I have written this to run my files simultaneously:

import multiprocessing

from file1 import my_function

if __name__ == '__main__':
    __spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)" # This line is because I am using Spyder as my IDE

    files = ["path/to/file1.py","path/to/file2.py","path/to/file3.py","path/to/file4.py","path/to/file5.py"]

    for item in files:
        p = multiprocessing.Process(target=my_function, args=(item,))
        p.start()
        p.join()
        print("Done!")
The issue is that, however, the files are still being run one after another like in a sequence but not in parallel! Do you have any ideas how I may run my files concurrently?
Reply
#4
The code I posted, now modified to print each process that is still running, will tell you what is running. Post back the results of the code I posted above.
Reply
#5
(Sep-12-2018, 12:15 AM)woooee Wrote: The code I posted, now modified to print each process that is still running, will tell you what is running. Post back the results of the code I posted above.

It does not work in parallel and consumes same amount of time. Plus all I get as the final output is:

--------------------------------------------------
finished
Reply
#6
From Multiprocessing - RTM

Quote:join([timeout])
If the optional argument timeout is None (the default), the method blocks until the process whose join() method is called terminates. If timeout is a positive number, it blocks at most timeout seconds. Note that the method returns None if its process terminates or if the method times out. Check the process’s exitcode to determine if it terminated.

A process can be joined many times.

A process cannot join itself because this would cause a deadlock. It is an error to attempt to join a process before it has been started.

You essentially block the parent process till the current child process terminates, making all the multiprocessing effort redundant.

You may wait on each process after your start all the processes. Drop p.join() from the first loop and use p.join(0.2) in the second one (also, drop time.sleep from it - redundant).


In general, I believe process pool could be a better option (haven't done multiprocessing in Python for ages) - but in your case that is not so important. If you apply the fixes I've specified above, your code should work as you expect it to.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in running a code akbarza 7 634 Feb-14-2024, 02:57 PM
Last Post: snippsat
  python convert multiple files to multiple lists MCL169 6 1,535 Nov-25-2023, 05:31 AM
Last Post: Iqratech
Question Need Help with Vehicle Routing Problem with Time Windows (VRPTW) in Python kasper321421312 1 554 Nov-10-2023, 08:19 PM
Last Post: snippsat
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 1,017 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  splitting file into multiple files by searching for string AlphaInc 2 888 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Processing Files that are not in use randywberry 3 675 Jun-06-2023, 06:00 PM
Last Post: rajeshgk
  Merging multiple csv files with same X,Y,Z in each Auz_Pete 3 1,151 Feb-21-2023, 04:21 AM
Last Post: Auz_Pete
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 957 Feb-15-2023, 05:34 PM
Last Post: zsousa
  Find duplicate files in multiple directories Pavel_47 9 3,075 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  Multiple.ui windows showing at the same time when I only want to show at a time eyavuz21 4 1,028 Dec-20-2022, 05:14 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020