[英]Make a Python Script that can run two python scripts concurrently
我有兩個要運行的 python 程序。 我想制作一個同時執行這兩個腳本的python腳本。 我怎樣才能做到這一點?
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()
您還可以使用concurrent.futures
庫中的ThreadPoolExecutor
並更靈活地確定應該生成多少工作人員來執行您的目標腳本。 像這樣的事情應該沒問題:
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)
這樣您就不必手動啟動線程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.