簡體   English   中英

如何從一個父腳本的不同文件夾運行多個腳本

[英]How to run multiple scripts from different folders from one parent script

我的父腳本: D:\\python\\loanrates\\Parent\\parent.py

子腳本之一: D:\\python\\loanrates\\test\\test.py

這是我的父腳本,該腳本導入並嘗試運行test.py

import sys
import thread

sys.path.append('/python/loanrates/test')

import test

thread.start_new_thread(test)

它可以很好地導入test.py ,但是我在使用thread.start_new_thread(test)運行時遇到了麻煩

測試腳本不包含任何功能,只是一個簡單的腳本,將.json保存到test.py的目錄中,為完整起見,我在這里粘貼:

import json 
data = 'ello world'
with open( 'D:/python/loanrates/test/it_worked.json', 'w') as f:
    json.dump(data, f)

最終,我將運行大約15個子腳本。 但是正如我所說,我在運行多個線程時遇到麻煩

除非您有充分的理由使用低級“線程”模塊,否則建議使用“線程”模塊,后者可以檢測到開發人員在擊鍵時的恐懼。 有了Threading,您將擁有更好的運氣。

#parent.py
from threading import Thread
import sys
sys.path.append('python/loanrates/test')
import test

t1 = Thread(target=test.threadTest())
t2 = Thread(target=test.threadTest()) #replace with other children
t1.start()
t2.start()

|

#test.py (the child)
import json,sys

def threadTest():
    #sys.path.append('python/loanrates/test')
    data = 'ello world'
    with open( 'it_worked.json', 'w') as f:
        json.dump(data, f)

注意,您可以設計threadTest()使其接受由parent.py傳遞的參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM