I have two python programs that I want to run. I want to make a python script which execute the two scripts concurrently. How can I do that?
-
You would need more than one processor. You could then run multiple processes concurrently (docs.python.org/3.7/library/multiprocessing.html). You might actually want multithreading depending on what you're trying to do (medium.com/@nbosco/…).NotAnAmbiTurner– NotAnAmbiTurner2018-11-21 01:37:10 +00:00Commented Nov 21, 2018 at 1:37
-
@NotAnAmbiTurner: You don't need more than one processor, just an OS that supports multitasking.martineau– martineau2018-11-21 02:21:14 +00:00Commented Nov 21, 2018 at 2:21
-
@martineau but then is it truly concurrent?NotAnAmbiTurner– NotAnAmbiTurner2018-11-21 02:46:02 +00:00Commented Nov 21, 2018 at 2:46
-
@NotAnAmbiTurner: No, technically not—but depending on what the tasks are actually doing it may not matter. i.e. Such as when one is doing I/O and therefore spending a lot of its time waiting for physical devices. multitasking would prevent that from hanging the entire system down.martineau– martineau2018-11-21 03:04:27 +00:00Commented Nov 21, 2018 at 3:04
-
But is there a script to run two python scripts concurrently?Armster– Armster2018-11-22 20:43:15 +00:00Commented Nov 22, 2018 at 20:43
|
Show 1 more comment
2 Answers
You can also use a ThreadPoolExecutor from the concurrent.futures library and be more flexible on how many workers should be spawned to execute your target script. Something like this should just fine:
from concurrent.futures import ThreadPoolExecutor as Pool
import os
n_workers = 2
def target_task():
os.system("python /path/to/target/script.py")
def main(n_workers):
executor = Pool(n_workers)
future = executor.submit(target_task)
if __name__ == "__main__":
main(n_workers=n_workers)
This way you won't have to start your threads manually.