[英]How to run multiple scripts from different folders from one parent script
My parent script: D:\\python\\loanrates\\Parent\\parent.py
我的父脚本:
D:\\python\\loanrates\\Parent\\parent.py
One of the child scripts: D:\\python\\loanrates\\test\\test.py
子脚本之一:
D:\\python\\loanrates\\test\\test.py
This is my parent script which imports and to (attempts) to run test.py
: 这是我的父脚本,该脚本导入并尝试运行
test.py
:
import sys
import thread
sys.path.append('/python/loanrates/test')
import test
thread.start_new_thread(test)
It imports test.py
fine, but i'm having trouble running it using thread.start_new_thread(test)
它可以很好地导入
test.py
,但是我在使用thread.start_new_thread(test)
运行时遇到了麻烦
The test script contains no functions and is just a simple script that saves a .json to test.py's directory, for completeness i'll here is a paste: 测试脚本不包含任何功能,只是一个简单的脚本,将.json保存到test.py的目录中,为完整起见,我在这里粘贴:
import json
data = 'ello world'
with open( 'D:/python/loanrates/test/it_worked.json', 'w') as f:
json.dump(data, f)
Eventually I will be running around 15 of child scripts. 最终,我将运行大约15个子脚本。 But as I said i'm having trouble running multiple threads
但是正如我所说,我在运行多个线程时遇到麻烦
It is recommended to use the "Threading" module unless you have a very good reason to use the low level "thread" module, the latter of which can detect fear in the keystrokes of the developer. 除非您有充分的理由使用低级“线程”模块,否则建议使用“线程”模块,后者可以检测到开发人员在击键时的恐惧。 You will have much better luck with Threading.
有了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)
Notice you can design threadTest() so that it accepts arguments, passed by parent.py. 注意,您可以设计threadTest()使其接受由parent.py传递的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.