[英]Make a Python Script that can run two python scripts concurrently
I have two python programs that I want to run.我有两个要运行的 python 程序。 I want to make a python script which execute the two scripts concurrently.
我想制作一个同时执行这两个脚本的python脚本。 How can I do that?
我怎样才能做到这一点?
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()
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.您还可以使用
concurrent.futures
库中的ThreadPoolExecutor
并更灵活地确定应该生成多少工作人员来执行您的目标脚本。 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.这样您就不必手动启动线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.