1

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?

6
  • 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/…). Commented Nov 21, 2018 at 1:37
  • @NotAnAmbiTurner: You don't need more than one processor, just an OS that supports multitasking. Commented Nov 21, 2018 at 2:21
  • @martineau but then is it truly concurrent? Commented 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. Commented Nov 21, 2018 at 3:04
  • But is there a script to run two python scripts concurrently? Commented Nov 22, 2018 at 20:43

2 Answers 2

2
import os

import threading

def start():
    os.system('python filename.py')


t1 = threading.Thread(target=start)

t2 = threading.Thread(target=start)

t1.start()

t2.start()
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.