[英]In Python, how can I run another Python script from the main script at the same time and close it when I stop the main script?
import subprocess
subprocess.Popen('python', 'second_script.py')
這會打開第二個腳本並使它們同時運行嗎? 如果我停止主腳本,它還會關閉第二個腳本嗎? 如果沒有,我該怎么做?
我不清楚你需要什么。
如果你想在另一個腳本“pythonScriptB.py”中使用一些在“pythonScriptA.py”中編碼的函數,你可以導入第一個腳本:
from pythonScriptA import *
# Use the functions in the ScriptB
如果您希望兩個腳本同時/並行運行,您應該查看多處理庫,或者查看允許 python 代碼在多個線程上運行的任何其他庫。
-- 編輯:更正腳本
你應該做這樣的事情
#! /usr/bin/env python3
import subprocess
pid = subprocess.Popen(['second-script']).pid
print(f'pid={pid}')
...
# do whatever you have to do here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.