如何从一个父脚本中的不同文件夹运行多个脚本

时间:2016-02-21 10:13:35

标签: python multithreading multiprocessing

我的父脚本: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个子脚本。但正如我所说,我在运行多个线程时遇到了麻烦

1 个答案:

答案 0 :(得分:0)

建议使用"线程"除非你有充分的理由使用低级"线程"模块,后者可以检测开发人员按键中的恐惧。线程的运气会好得多。

#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传递的参数。

相关问题